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

Cannot understand PezDispenser dispenser = new PezDispenser(); (6:30 in video)

Hi all. I have listened to the same 20 seconds over and over and I still haven't figured out what this line is really about. I understand there is a new variable being made called dispenser but I do not understand how the command 'new PezDispenser()' works and why its used like that.

Even though Craig's mentoring skills are good I do not understand this explanation "We instantiate a new PezDispenser objects and creates new instance. This object here is refered to as a instance of a type PezDispenser."

6 Answers

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hmmm... almost... a little bit more...

PezDispenser dispenser = new PezDispenser("Yoda");

means:

  1. Create a new object called dispneser.
  2. Set the attributes and features as specified in a class called PezDispenser
  3. Set the head to be a "Yoda" character

Please note "Yoda" nameCharacter is user defined. You can enter "Darth Vader" and process above run all over again but then the head produced will be "Darth Vader".

I hope this will help

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi There...

I will try to explain to you in the most practical way possible but please do not refer my explanation as formal guidelines okay. This is just like telling what Craig said from my perspective using easier language for me to understand what he meant. I am still learning also so please consider this a new way of seeing things.

Okay let us start. This is the code in question:

PezDispenser dispenser = new PezDispenser();

since you already wrote this code I assume you already made two files Example.java and PezDispenser.java with PezDispenser class in it. This PezDispenser class is important in our story so please pay attention to it. But first I want to correct something when you said you create a variable named dispenser, which is wrong. I trying so hard to avoid word instantiate so I just want to tell you that using that code you are CREATING an Object called dispenser.

What is so special with an object? dispenser stored data, why can I just called it a variable? Well here is where the story begin to be a little bit twisty. Object has more to offer than just storing data, it can offer pre-determined methods that will allow you do many amazing things with it. You can check if the dispenser is empty. Simulate refill the dispenser. Or if you will you can even make simulation on how to blow up a PezDispenser if it was an object. The formal definition is more elaborate but you understand this is so it is easier to understand.

Okay back to our code. Basically it tells the compiler (the machine that transform your code into machine language) that you want to INSTANTIATE (create) an object called dispenser. This object called dispenser has all attribute (those amazing gimmicks of things I told you before) the same as described in a class called PezDispenser. BUT I want to make NEW fresh object of this dispenser so that if some other user or some other command ask the compiler to make dispenser the compiler will not just handed them your already established dispenser. Instead the compiler will make a new object which also called dispenser but different from yours. It is just like if you buy a real life PezDispenser. The other guy after you can also buy a Yoda headed PezDispenser with the same specifications (size of dispenser, head, and color) but it is not yours and vice versa yours are not theirs.

in future lessons you might come across similar statement but without using new. In this case it means the object was passed an object that already created. However, this is for later okay because I still cannot find easier terms to tell this explanation.

I hope this will help you a little. Please remember this is not a formal explanation some aspects might not as elaborate as I wish it will. Any feedback and corrections will be much appreciated.

Nevena Miskovic
Nevena Miskovic
535 Points

Hi, I have exactly the same question and I read the answers and still not totally clear with all the terms. I wish the Teacher had been more explanatory with the all the new terms instead of just leaving it there and waiting for us to figure out elsewhere. The whole "no previous programming knowledge required" feels more and more clumsy as I progress throughout the course.

Thank you very much for giving me such a thorough answer. Much appreciated.

Just to make sure I get it (my head can be thick at times).

PezDispenser dispenser = new PezDispenser(); means

Look for class 'PezDispenser' and check what it is (In this case the string "Yoda"). Now let it be the object 'dispenser' but make a unique one.

Thanks again!

Md shujaul Haque
Md shujaul Haque
839 Points

workspace not working properly code on loopy not compiling its given error