Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Meet Objects Creating Classes

Chris Tvedt
Chris Tvedt
3,795 Points

Need help understanding the "dispener.mCharacterName" part of the code!

To see if i got this right.

The line of code - PezDispenser dispenser = new PezDispenser(); does the following:

  • It makes and object called "dispenser" inside the "PezDispenser"-class. Correct?

When he then writes "System.out.printf("The dispenser character is %s\n", dispenser.mCharacterName);" the following happens:

  • Goes into the "PezDispenser"-class, finds the object "dispenser" and passes variable value of "mCharacterName" into the variable "dispenser". (??)

(In essence "dispenser = mCharacterName;" if they had been in the same class ??)

So that now the object "dispenser" carry the same value as "mCharacterName"?

Having a hard time understanding the "dispenser.mCharacterName" part really. I think i get the first part of making the object (that is the actual dispenser that gets passed the type of head, color etc.?).

Please help me understand this.


1 Answer

PezDispenser dispenser = new PezDispenser() 

I would say that the object is not called "dispenser" that is just the variable name that we choose to point to the object that exists in memory.

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

This doesn't go inside the class but inside the object itself which was created having the PezDispenser class as it's blueprint, therefore the object itself in memory has a member variable called mCharacterName which we can access via: object.propertyName

Then you wrote something confusing about "dispenser = mCharacterName" which I don't really understand, I think you are confused.

Maybe a stupid example might help: imagine we have a class human with a property name and nationality

Human h1 = new Human(); 
h1.name = "Chris"; 
h1.nationality = "Norwegian";

Human h2 = new Human(); 
h2.name = "Pedro"; 
h2.nationality = "Portuguese";

Here we have two objects of type Human that have 2 properties/member variables.

dispenser = mCharacterName; // this is wrong, it would be the same as telling h1 = nationality (they are just member variables/properties of the objects)

Chris Tvedt
Chris Tvedt
3,795 Points

Thank you! this made it so much more understandable!

So you made 2 objects out of the class Human, and passed in name and nationality in to each of the objects as they are made.

Thank you, now im of to understand how i can get input from a user, in the "Example"-class and pass it to the "PezDispenser"-class so that the user can decide the mCharacterName them selves :-)