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) Harnessing the Power of Objects Helper Methods and Conditionals

Lucas Caldeira
Lucas Caldeira
977 Points

PezDispenser dispenser = new PezDispenser("bruce"); What does de keyword "new" do ? thankyou !

Everything else I undertood ,just could not get this one .

loving treehouse classes so far ! thakyou !

2 Answers

Harry James
Harry James
14,780 Points

Hey Lucas!

The new keyword is used to create a new instance of something.

So, what were doing in this line of code is creating a new instance of the PezDispenser class. It makes sense to think of it like this: Our PezDispenser class doesn't yet exist in our code so, we need to create a new instance of it. We can't just call PezDispenser on its own as, it hasn't yet been created and Java doesn't know what it is!


One thing to note in the future though, to access some methods, we'll need to create a new instance of their class before we can use methods from that class.

This doesn't always apply though - we can have static methods. These methods don't need to be instantiated so, we can use them straight from our code (However, an import is needed when it's outside of our package or, if the package it is called directly off the class like MyClass.myMethod()).


When you call a new instance of an object, it is what it says, a new instance. So, Java treats it like this and, often in debugging, you'll see each instance is given a unique code.

A static variable or method however, only has one instance. So, even if you called it 100 times, there'd still only be 1 instance of it existing.


Hopefully this makes sense but, if there's anything you don't understand, I'll try my best to explain it :)

Lucas Caldeira
Lucas Caldeira
977 Points

thankyou so much, it all makes sense now .

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Lucas;

Great question! The new operator instantiates a class (creates an object) by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

As we have learned, we define variables and objects using the type name; syntax. This tells Java that name will be used to reference an object of the type type.

We can assign a name to an object(variable) without giving it a value like PezDispenser bruce; and Java will hold that as a reference variable but it's value will be undetermined until an object is created with the new keyword.

I hope that makes some sense, post back if it does not.

Ken

Lucas Caldeira
Lucas Caldeira
977 Points

It makes sense now tankyou so much