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

Mahvish Irfan
Mahvish Irfan
449 Points

Why did he write "dispenser.mCharacterName" instead of just "mCharacterName" like he used to in the previous videos?

Please let me know. I've watched this more times than you could imagine and am still pretty confused?

7 Answers

Per Karlsson
Per Karlsson
12,683 Points

Hi Mahvish,

Roughly 3:50 into the video Craig creates a new class called PezDispenser. In it he creates a public field called mCharacterName. This field or member variable is accessible outside the class (PezDispenser).

Roughly 6 minutes into the video Craig creates an instance or object of the PezDispenser class in the Example class. He calls it dispenser (he could've called it pretty much anything, like pez for example).

Creating this instance or object of the PezDispenser class let's him access the mCharacterName field. Hence he types out dispenser.mCharacterName to get the value, in this case Yoda.. Hope this explains it :)

~Per

Anas Rida
Anas Rida
8,183 Points

But wouldn't it mean that "dispenser" object is created in PezDispenser class, how is he able to access it in Example class??

NaiShiuan Zheng
NaiShiuan Zheng
18,202 Points

Craig has two classes in this example, one is Example.java and the other is PezDispenser.java , mCharacterName is a field(variable) that belongs to the class PezDispenser , not the class Example , so he cannot just only type mCharacterName because it doesn't exist in the Example class.

In order for the Example class to use the mCharacterName field(which belongs to the class PezDispenser), he has to "new"(create) an object called dispenser of the class PezDispenser (Line 6 in Example.java) , then he can use this dispenser object to access the mCharacterName field by typing dispenser.mCharacterName.

You can think dispenser as a object(lets say a dispenser) , and mCharacterName as a name (or the head of a dispenser) and consider dispenser.mCharacterName as a dispenser that has a head of master yoda. I Hope this explanation helped you

Anas Rida
Anas Rida
8,183 Points

but how is he able to use a PezDispenser object in Example??

NaiShiuan Zheng
NaiShiuan Zheng
18,202 Points

In your Example class you will write ---> PezDispenser dispenser = new PezDispenser();

the above line of code creates an object of the class PezDispenser, so once it is created , you can use this object in the Example class. Is this what you are asking?

Mahvish Irfan
Mahvish Irfan
449 Points

Per Karlsson i think i kind of get it now ... :D

NaiShiuan Zheng
NaiShiuan Zheng
18,202 Points

yes you are right! dispenser.mCharacterName does mean getting or accessing the value of whatever you assign to the mCharacterName , in this case, it is "Yoda". You can try changing Yoda to "Vadar" or "Luke" for example to verify. It is always good practice to test out many things to make you understand more about this concept, just remember that the value that you assign to mCharacterName has to be a string.

NaiShiuan Zheng
NaiShiuan Zheng
18,202 Points

rather saying that the dispenser object is an imaginary copy of PezDispenser, you should think of it as this

the PezDispenser class is a blueprint to make dispensers. whatever details you give to the PezDispenser class will affect the dispenser that you create. For example giving the PezDispenser the attribute/field mCharacterName of Yoda is essentially saying to follow the blueprint that all the dispensers that you create will be the model of Yoda. If you gave the PezDispenser class another field called dispenserColor = "green" , the dispensers that you create out of this class will all be green.

The PezDispenser is the blueprint the variables inside the class are NOT objects the dispenser that you created is an object , the actual product. You manipulate fields/attributes via the object not the class itself.

Getting into this much detail to the point that I really don,t know if I intepreted it right but hope this helps............

Chris Tvedt
Chris Tvedt
3,795 Points

Thank you for all your good answers! This helped alot! :)

Per Karlsson
Per Karlsson
12,683 Points

Sorry to hear that Mahvish Irfan. Don't know how else I should explain it :<

Mahvish Irfan
Mahvish Irfan
449 Points

Thanks for your help anyway :) I appreciate it.

Chris Tvedt
Chris Tvedt
3,795 Points

To see if i got this right. and it might help others later on.

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);"

What he does is go into the "PezDispenser"-class, finds the object "dispenser" and gives that variable the data from "mCharacterName".

In essence "dispenser = mCharacterName;" ?

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

Haveing 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.

Chris

NaiShiuan Zheng
NaiShiuan Zheng
18,202 Points

The concept that dispenser.mCharacterName means dispenser = mCharacterName is incorrect , The PezDispenser class is basically the blueprint of your object meaning that if you give attributes to a class, the object you create from that class will be able to get access the attribute,

In this video, The PezDispenser class is given an attribute called mCharacterName, named "Yoda" right ?.

public class PezDispenser { public String mCharacterName = "Yoda"; // you are telling everyone that your dispenser is the character of Yoda }

In real life you can interpret it you giving a PezDispenser a head of Yoda,

System.out.printf("The dispenser character is %s\n", dispenser.mCharacterName) is basically trying to say that your dispenser that you created has the head of Yoda

so dispenser.mCharacterName is like you saying what character is on your dispenser object.

I think you are mainly confused with the . (dot) in the java code, it just refers to the attribute that belongs to the dispenser.

Hope this reply helps you

Chris Tvedt
Chris Tvedt
3,795 Points

Ok, understand it abit more now, but what makes "dispenser.mCharacterName" be "Yoda" and not just mCharacterName? Is it because you see the object, "dispenser", that is made from the class(blueprint) Pezdispenser, and inside that class there is a String called mCharacterName with the value "Yoda"? So dispenser.mCharacterName is in essence the code telling the program to "enter" the blueprint of the object "dispenser" and get the value from mCharacterName? Or is the "object" really an imaginary copy of PezDispenser, so that all variables inside PezDispenser is inside the object aswell? because i don not see that the code tell the program to enter the PezDispenser class to retrive mCharacterName, but to enter the object made from PezDispenser called "dispenser" to get the mCharacterName.