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

Benneth Paredis
Benneth Paredis
4,597 Points

I'm so confused can someone help me explain.

"PezDispenser dispenser = new PezDispenser();"

can someone tell me what this line of code does?

@Benneth Paredis: I also struggled to understand what part of this code was doing what. I wish Craig would've walked us through that. Since each part has a very similar name, it makes it extra difficult to see what the heck's going on.

7 Answers

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hi Benneth,

on the left side you have the declaration part. The first PezDispeser is the class name. Then the variable name "dispenser.

On the right side you have the instantiation, The new keyword is used to create a new object of the class PezDispenser. You can also say a new instance of the PezDispenser class. The second PezDispenser is the name of the constructor (tjis is a reason why constructors have same names as the class) that is used for the object creation.

Makes sense?

Grigorij

Thanks for your help

Grigorij Schleifer : How did you learn what you know to be able to answer this question because Craig doesn't explain this core concept in his video on Creating Classes?

Roger Kisekka
Roger Kisekka
680 Points

i have a hard time understanding also... PezDispenser dispenser = new PezDispenser();

am i understanding this correctly the first part is basicly the same as

String dispenser

int dispenser

PezDispenser dispenser

telling what type dispenser variable gets, and in this exampel it gets the "class PezDispenser".

and then the new makes it possible to write like dispenser.mCharacterName but the last thing i dont understand what it means "name of the constructor"

can someone elaborate this and tell me if im thinking right with the other things?

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hey Roger,

you are right with your thoughts :)

The constructor gets called when we create an object of a class (i.e. new keyword followed by class name).

To create a new instance of the class (here the "dispenser" is the instance or object of the PezDispenser class) the compiler uses constructors. The constructor can take arguments/attributes like the color/capacity/taste of the Pez etc. of the PezDispenser and "assign" it to a new "dispenser".

If you don´t define a constructor inside a class the compiler creates a default (invisible) one (same name as the class) and instantiates a new object without attributes.

Makes sense?

Grigorij

Benneth Paredis
Benneth Paredis
4,597 Points

Now i understand thnx for explaining

Roger Kisekka
Roger Kisekka
680 Points

Thank you so so so so much Grigorij i think i really got it now :D the best help ever.... but just to recap

PezDispenser coolPezDispenser = new PezDispenser("Cool PezDispenser");

to break it down, hope u get what i mean :)..

*the first PezDispenser referse to the class

*the coolPezDispenser gives the variable a name so i can "use it in the code"

*the "new PezDispenser" refers to the "public PezDispenser(String characterName)" constructor and the attribute of the constructor is "characterName = mCharacterName" and it gets access to the private variable, and it changes the value of the private variable "private String mCharacterName;" and gives it the name/value of "Cool Pezdispenser"

am i understanding it correctly now?

i cant give ur answer a vote up :/

Chris Tvedt
Chris Tvedt
3,795 Points

Not "characterName = mCharacterName", this will give the variable"characterName tha value of "mCharacterName", you want it the other way around ;)

Roger Kisekka
Roger Kisekka
680 Points

Grigorij Schleifer Thank you so much for the help!!! u are awesome

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hey Roger,

thank you so much for your kind words : )))

You can mail to Faye Bridge (faye@teamtreehouse.com) and suggest me as a Moderator :sunglasses: :smiley:

This would be so cool !

See you in the forum bro ...

Grigorij

Benneth Paredis
Benneth Paredis
4,597 Points

Now i understand thnx for explaining

Roger Kisekka
Roger Kisekka
680 Points

I think im starting to get it. u are saying that the "constructor can take arguments/attributes like the color/capacity/taste of the pez"

so the second PezDispenser(); makes it possible to take the arguments from the PezDispenser class that i define like the i.e

"public String mCharacterName = "Yoda";"

and btw so something that may help me is a answer to this.... lets say i have "class inside a class" is it possible to type like ""PezDispenser dispenser = new SecondPezDispenser();"" is that a possible way to do it to redirect to the class inside the class if we have a nested class, or am i on the wrong track now?

i hope im getting this or otherwise i feel really stupid :( sorry that im bothering you with this thing, its the only thing that i cant get in my brain :P

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hey Roger,

you are not stupid !!!

Learning programming is really frustrating (except python !) Python is coooool ...

And you are not bothering me at all :) I love answering questions and help peaple out ;)

OK ... the new keyword call the PezDispenser class for instantiation of the new object. And a class has a defined or default constructor that can take attributes.

public class PezDispenser {
// your class as the blueprint for new objects
private String mCharacterName;
// the blueprint has a variable mCharacterName
// make it private to encapsulate/protect the variable
// so you can access the method through getter methods
// or assign values using the constructors attributs when you create a ne object of the PezDispenser

public PezDispenser() {
// constructor without attributes 
// a default constructor would look like this
}

// if you dont create a constructor a default(invisible) constructor 
// will be created to instantiate a new PezDispenser object without attributs

public PezDispenser(String characterName) {
//constructor with an attribute (here the name of the new PezDispenser object)
mCharacterName = characterName;
// assigning value of the attribute to the private variable
}
}

So if you create a PezDispenser object and give it a name "Cool PezDispenser")

It looks like this:

PezDispenser coolPezDispenser =  new PezDispenser("Cool PezDispenser");

I hope someone can help you with the nested classes question.

Don´t hesitate to ask :)

Grigorij