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 Privacy and Methods

I don't understand this line of code: PezDispenser dispenser = new PezDispenser();

Are we creating a new method here? Can someone please explain what each item in this line of code is? I guess PezDispenser is actually referring to the PezDispenser.java object, but I don't understand what is getting assigned to what in this line of code.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Does this make sense:

When assigning to a variable for the first time, you must declare what type of object it will hold;

<object type> <variable name> = <expression resulting in an object>

When assigning to a variable the For the line:

PezDispenser dispenser = new PezDispenser();

"PezDispenser" is the object type that will be assigned to the variable name "dispenser".

The expression "new PezDispenser()" is a call to the class "PezDispenser" to create a new instance of "PezDispenser" and return that instance. This instance will be assigned to the variable dispenser.

Thanks. I sort of get what you are saying on the assignment to the variable "dispenser", but I guess I'm confused about the difference between the object type "PezDispenser" and class "new PezDispenser()".

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Java requires that all variables have a declared type. Variables are simply references to objects and must have the same type as the objects they refer to. So while this seems it's from the Department of Redundancy Department, you must declare an variable to be of the same type as the newly created object of the same type before assigning a reference of to the new object to that variable.

Like creating a new empty string would be written as:

String bob = new String();

bob is a reference of type String, that refers to the newly created object returned by the new String()

I don't know if this is helping, or if I'm saying the same thing with different words.

Antonio Ruiz
Antonio Ruiz
3,072 Points

I did exactly what Craig did and get this error:

Example.java:5: cannot find symbol
PezDispenser dispenser = new PezDispenser();
symbol: class PezDispenser

Any idea why?

Exact question i had. Thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

When compiling using javac Example.java, the file PezDispenser.java must be present. When found, PezDispenser.java will automatically be compiled as well. Try creating the file PezDispenser.java with content:

public class PezDispenser {
    // member variable -- field -- name starts with lowercase "m"
    private String mCharacterName;

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

    // acccess ReturnType
    public String getCharacterName() {
        return mCharacterName;
    }
}