Train with a Trainee: Week 2 Part B, Classes and Objects

3

I hope everyone was able to get through Part A without any trouble. Of course, as I mentioned before, what we just went through and created won’t do anything yet. That is where Part B comes in. Don’t worry it won’t be as intense as part A was.

First things first though. We want to select the Store.java file now, since we are done working inside the book.java file. Now we can take a look at our code:

package books;

public class Store {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
book book1 = new book(“Joe Smith”, 2010,”History of Android”);
book1.printValue();
}
}

See I told you. Much less here to worry about. What you see here is that we are obviously still working inside package books.

The first change you notice is the public class Store { line. If you look and think about this it will make sense. We changed to the Store.java file. So now our class is Store

These are our two new lines.


book book1 = new book(“Joe Smith”, 2010,”History of Android”);
book1.printValue();

The first word book links this back to public book(String iniAuthor, int iniYearOfPublication, String iniName ){ which is in our book.java file. The second word book1 tells the system that this is the first book for the application to reference. This is also called the object.

Now we have = new book(“Joe Smith”, 2010,”History of Android”); new tells Java we are creating a new object.The word book means we are creating a new book. The following string (“Joe Smith”, 2010,”History of Android”); gives us the information that is needed to fill in our variables. If we go back to the book.java file you will see this string
public book(String iniAuthor, int iniYearOfPublication, String iniName ) remember? So the string (“Joe Smith”, 2010,”History of Android”); fills that info. Joe Smith is the String iniAuthor, 2010 is the int iniYearOfPublication and History of Android is the String iniName.



The second line book1.printValue() tells the application to print the defined values how we have defined them in the book.java file.

If we run this application, your OUTPUT window will display the following:




We have now created a JAVA application that referenced an object to give our information.

As always I made a few modifications to help myself learn. Here is my book.java code

my Store.java side
Here is what the OUTPUT window displays for me:
I want one of those cars so bad. LOL.

I located a Youtube video that demonstrates and explains this pretty well. It uses a different example but the same principles. Feel free to use the example in the video also. It will help get the association down. As I can’t use my actual training videos for everyone, I will try and locate something on Youtube that will help you see a visual explanation. That is how I learn best. I will also include them at the end of a segment so everyone has a chance to do it on their own.

Here’s a great video though of what Classes and Objects are and how they are used.








Share This Article....

About the Author

Stormy Beach
Time for an update. I am the proud father of three, 2, 8, and 11. I finally just moved up from the Samsung Vibrant to the Samsung Galaxy S III...... Let me tell you, it is mind blowing. I love Android, I love tech and I love my family. Not in that order though. I work to hard, play even harder and take care of all that are around me when I can.

3 Responses

  1. Jsadfasf says:

    Man thanks for the tutorials, I check the website all the time to see what is next :]

Leave a Reply

© 2010 AndroidSPIN. All rights reserved.