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

Safira Nugroho
Safira Nugroho
3,910 Points

Why "new PezDispenser()"?

So I understand that PezDispenser is the variable type and dispenser is the variable itself, but what does Craig mean when he says "We're gonna make a brand new PezDispenser"? I don't see "PezDispenser()" being used anywhere in Example.java, so I'm really having trouble understanding what that particular line does.

2 Answers

Hi Safira Nugroho,

In Java you have primitive types like int, float, double for example and types of reference to an object.

In order to create an object you have to create an instance of the specific class from which you want to create an object of. That may sound very complicated but it is pretty simple.

// declaration of a variable
int number;

// initialisation if the variable number
number = 5;

/****************************************************/

// declaration of a variable
PezDispenser myOwn;

// initialisation of the variable myOwn
myOwn = new PezDispenser();

Those lines above could of course be shortened:

// declaration + initialisation

int number = 5;
PezDispenser myOwn = new PezDispenser();

But I think you can understand the process better with the first example. Like you see, you can initialise primitive types without the keyword new. For types which reference to an object you have to use the keyword new and call the constructor of that class which in this case is PezDispenser().

With the declaration of the variable you only let the compiler know that you have created a variable of a chosen type but it doesn't hold any real value with that step. It will hold a default value which is null and basically means it holds nothing.

Only with the initialisation your variable will represent a real value or object.

Hope I could help you out there a little bit. If you still have questions please don't hesitate to ask.

Safira Nugroho
Safira Nugroho
3,910 Points

Ah alright, I understand now! So the keyword new is essentially used to make the so-called product from the so-called blueprint, yeah?

Thank you so much!

Exactly! Great that you got it and you are welcome :-)

Mario, Thanks for the explanation, this helped me out a lot as well

Hi Mario - I think I am on the verge of grasping this. Is "PezDispenser" the object or the class? Is "myOwn" referencing the object "PezDispenser"? Then "PezDispenser()" is the constructor of the class "PezDispenser"?

I'm also confused as to why there are two files "PezDispenser.java" and "Example.java". Why can't all of this be within one file?

Sorry if I'm way off on my questions. This is my third time reviewing this Java Objects video and it is just. not. sinking. in.

Eric