Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Meet Objects Creating Classes

Where did the console find Yoda???

I can't understand, how did the console write that the character is Yoda, if it wasn't in a code.

1 Answer

Hi

In main, you did 2 things:

    first, you requested an object of type PezDispenser .   This is like going to a store and buy an Apple.   So now you have an Apple in hand and you called it myApple.   Instead of Apple think (PezDispenser ) .   In this case, you have a specific PezDispenser  in hand, and you are called it dispenser.

    PezDispenser dispenser = new PezDispenser();

The apple has a built in attribute .... Color. If it is a red apple, then Color = Red. Now think of your dispenser; dispenser has a character .... it called mCharacterName.

The way you refer to apple color is: tell me what is ........> apple.color

here you are saying tell me what is ........> dispenser.mCharacterName

    System.out.printf("the dispenser character is %s\n",
                    dispenser.mCharacterName);

since the dispenser object has an attribute mCharacterName = "Yoda"

then dispenser.mCharacterName will return "Yoda"

hope this helps.