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 Meet Objects Creating Classes

Teymuraz Kobaliya
Teymuraz Kobaliya
2,646 Points

Hello! What does line " PezDispenser dispenser = new PezDispenser; " really mean?

Hello! Can anyone do me a favour and break it into baby steps? What does line " PezDispenser dispenser = new PezDispenser; " really mean? I just don't understand what we are trying to do.

If I understand correctly, we create an object "dispenser" inside the class "Pezdispenser", but what comes after "=" sign?? What does word "new" stand for (is it class/object/whatever). ...and then we use class "PezDispenser" again. Why?

3 Answers

Fahad Mutair
Fahad Mutair
10,359 Points

hi Teymuraz Kobaliya , this line mean to create object of class PezDispenser and name it dispenser , the = and new mean to instantiate a class PezDispenser that will create an instance of that class.

PezDispenser dispenser = new PezDispenser();

you are creating a new object dispenser of class PezDispenser

Kurt Daisley
Kurt Daisley
4,228 Points

My understanding is that we're creating a new INSTANCE of the class PezDispenser, and, rather confusingly, are calling this new instance 'dispenser'. The question that I have is - at 5:11 in the video, Chris says that we're creating a new VARIABLE of type PezDispenser, and then later on in the video (around 6:13) calls this same thing an INSTANCE. Which is it? Are the words interchangeable?