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 Constructors

Ryan Canty
Ryan Canty
2,433 Points

PezDispenser.java... What is happening?

Hey guys! I'm new to Team Tree House and I am trying to learn java. The answer to this question may be really obvious, but I cant figure it out. Basically what is happening in this code? I think I know that the character name is passed into the "characterName" variable and that is passed into "mCharacter" and them that is passed into "getCharacterName". This might even be totally wrong, but why does this variable need to be passed all over the place?

public class PezDispenser { private String mCharacterName;

public PezDispenser(String characterName) { mCharacterName = characterName; }
public String getCharacterName() return mCharacterName;

Also, from the Example.java class why do you have to define a new PezDispenser For example, when you do

PezDispenser dispenser = new PezDispenser();

what does that do? why do you need to rename PezDispenser to dispenser? Does "dispenser" become an easier to specify the PezDispenser class as apposed to typing PezDispenser everytime? I cant figure that out.

4 Answers

Ryan Ruscett
Ryan Ruscett
23,307 Points

Hey,

Just saw your last question there. Let me say it like this.

PezDispenser dispenser = new PezDispenser

That PezDispenser is an object that get's created. Allowing you access to that classes or objects methods and variables. The reason I use dispenser and NOT PezDispenser is because I don't use the object out right. The line above does 2 things.

  1. It creates the object (PezDispenser)
  2. Creates a reference to the object (dispenser)

This of the reference like a remote control and PezDispsner as a TV. I control the TV via the remote control. So dispenser(remote) is used to control the class PezDispenser (TV).

When I create the class, the remote or reference is created containing all the methods. So like this.

If I have a tv class. I know a tv has on, off, channel change and volume.

TV remote= new TV();

What this says is TV that contains on, off, channel change and volume. I will create a reference called remote and give it the ability to do on, off, channel change and volume. So whenever I want to change that object. I say remote.volume().

Does that kind of help clear it up?

Ryan Ruscett
Ryan Ruscett
23,307 Points

Hey,

So we have a class called PezDispenser. The class holds onto a variable called mCharacterName. M is used to define that it's a member variable. Meaning any method in the class will be able to use the value of this variable once set. It's private so only methods within this class can use it. So in order to access the variable, I need to instantiate or create the class. That is what PezDispenser dispenser = new PezDispenser(); is for. More on that in a minute.

If I had mCharacterName defined in a method. The variable would only exist while the method was being run. By defining it above as a member variable or class owned variable, it's available while the class exists. Not just while running a particular method.

Next, we have a pezDispenser constructor. I know this because the constructor has the same name as the class. This means that when the class is created. The constructor is what creates it. Every class has a constructor, but if you do not define one. A no argument default constructor is called. Which doesn't do anything. It's there even if you don't see it. When you see a constructor it's because when the class is first built, it needs to be built a specific way. We do this using the constructor. I need to pass a String into the constructor, a characterName of type String. When the class is instantiated using PezDispenser dispenser = new PezDispenser(); I pass in a string name. This way, when the class is built for the first time. It sets the mCharacterName to whatever i passed into the constructor when I instantiated the class. Like below

PezDispenser dispenser = new PezDispenser('Ryan'); This creates the class below, and the constructor runs and assigns mCharacterName to my name, Ryan.

Then I have a method inside the class PezDispenser, that is getCharacterName(). When I call this method from the instantiated class, I can use the dot notation.

PezDispenser dispenser = new PezDispenser('Ryan'); -- creates the class and sets mCharacterName to Ryan via constructor

use the reference "dispenser" from the line above. To call getCharacterName.

dispenser.getCharacterName() -- this will return Ryan. Because when the class was created, I passed in Ryan as the characterName. Which was assigned to mCharacterName name by the constructor. The variable is private, so I can't just call it like a static variable. I need to instantiate the class, Call a method of that class because remember. Since it's private, only methods within the class can access the variable. So I instantiate the class. Set characterName to my name. Then I use dot notation using the reference to the class and the method in that class, to call the private member variable mCharacterName. This value get's passed back to the class I am working in and wallah, I have what I need.

Does this help clear up your confusion? At least for now? Let me know if not, because it is a concept hard to explain.

public class PezDispenser { 
      private String mCharacterName;

      public PezDispenser(String characterName) { 
             mCharacterName = characterName; 
}

      public String getCharacterName() {
           return mCharacterName;
}
Ryan Canty
Ryan Canty
2,433 Points

Thanks alot! You are right, this concept is pretty hard to understand but your reply cleared it up for me a lot. Thank you!

Ryan Canty
Ryan Canty
2,433 Points

Last question. Im pretty sure I get this now. But when I do

console.printf ("the Pez Dispenser will be %s", dispenser.getCharacterName());

why do I need to say "dispenser" instead of PezDispenser? My understanding is that the purpose of "dispener" is to specify the class that "getCharacterName" exists in? if this is true why would you not just use PezDispenser instead?

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Great explanation, Ryan !!!! THis helps a lot ...

Gudbergur Isleifsson
Gudbergur Isleifsson
1,042 Points

Thank you Ryan. This helps me understand this alot better. :D