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 New Objects

Sorawit Kongnurat
Sorawit Kongnurat
1,284 Points

I don't understand Java Objects after going through it twice already.

I don't really understand the concept that is being taught in Java Objects. The first section in Java was much easier to understand however in Java Objects barely anything got to my brain. Yes, I was able to finish all the quiz but I don't feel comfortable with it.

PezDispenser dispenser = new PezDispenser ("Mapring");

In this line I understand that dispenser is the variable. PezDispenser is the object but what significant does it serve? You could just treat me as if I know nothing about this line actually.

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

I don't understand why dispenser needed to be combine with .getCharacter. Is that a way to pass the variable from the above line into this line?

public class PezDispenser {
  private String mCharacterName;

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

  public String getCharacterName() {
    return mCharacterName;
  }
}

All of this is just so confusing actually. I don't really know howto explain which part I don't understand because I just don't get it sadly. Any help would be appreciate. Is it best if I go back through it one more time...

2 Answers

Hi Sorawit,

You've clearly learned more than you think! Your understanding is not far out at all.

Let's take your questions in turn. First, you're asking what is:

PezDispenser dispenser = new PezDispenser ("Mapring");

This line creates a new instance/object of a PezDispenser. That object is called dispenser but you can call it whatever you want. That instance, and any other instances you create, have certain properties, defined within the class. The class is the template from which all PezDispensers are made.

Currently, the PezDispenser class has one stored property/member variable - that means every instance of the class has that property too. That is mCharacterName. When you created this dispenser you assigned the value "Mapring" to that member variable/property. (that's what constructors do, they set up your instance with values you pass to them). The access rights to the property, mCharacterName, are set to private meaning you can't mess with it directly; you need to use the accessor methods, also called getter and setter methods. One of these is getCharacterName() and it does just that - it goes off to the instance and gets the value stored in mCharacterName. For your instance, it would send back "Mapring".

Every instance of PezDispenser has this method. So, to let the compiler know which instance you want to name of, you chain the name with a dot, then the method name. So, the bit of code you were questioning, dispenser.getCharacterName(); is asking for the value stored in mCharacterName in the instance dispenser.

Had we created another instance and called it, anotherDispenser then we have two instances, and the method would work on both meaning, anotherDispenser.getCharacterName(); would return whatever was stored in mCharacterName in the anotherDispenser instance.

Let me know if that makes sense - and, obviously, feel free to ask any other questions.

Steve.

Sorawit Kongnurat
Sorawit Kongnurat
1,284 Points

I am starting to get a clearer image of this concept but I will still need to play around with it more. Thank you for your explanation and if I got any more question I will definitely post them in the community forum.

Jacob Klug
Jacob Klug
469 Points

What does Mapring do?