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 Access Level Modifiers

Ismayil Aliyev
Ismayil Aliyev
18,560 Points

Hello everyone, When we create new object Is type of object and name of class to be the same?

PezDispenser dispenser = new PezDispenser();

Am I correct in statements below?

PezDispenser is type of object. dispenser is name of object. new means we are creating new object. and PezDispenser() is method which refer to class that we created and it's name should be the same with class.

Christopher Janke
Christopher Janke
11,054 Points

I think I understand what you are trying to ask. I will tell you what is happening and see if this helps you.

so you have PezDispenser dispenser = new PezDispenser();

The first PezDispenser is simply stating what type of object you are declaring

the name dispenser is simply the name you are calling it. It could be whatever you wanted it to be but generally it is a good idea to keep it the same, and in line with whatever language you are using naming conventions.

when you use = new PezDispenser() you are initializing it in whatever scope of the method you are using. So you are giving the method you use this in access to the class PezDispenser.

I hope this makes sense.

yeah the class name specifies the object type for example the line of code: assume that there is a class named animal. PezDispenser dispenser = new PezDispenser()

so here PezDispenser specifies that dispenser is an object of type PezDispenser.

the function PezDispenser() has the same name as its class.

2 Answers

gmilan
gmilan
9,287 Points

Yes. In this example PezDispenser is the class, and despenser is the object of the class PezDispenser. PezDispenser has a constructor that is created in the PezDispenser class folder. . You can create many different constructor attributes in your PezDispenser class folder, and despenser object can use all of them.

Ismayil Aliyev
Ismayil Aliyev
18,560 Points

Christopher thanks for response. "The first PezDispenser is simply stating what type of object you are declaring" Exactly, but could we name type of object somehow differently?

Christopher Janke
Christopher Janke
11,054 Points

Yes you can name it whatever you want. You could name it PezDispenser cuteCatz or PezDispenser funnyFrogz. Please note though, that when programming larger programs with many different classes it starts getting hard to keep track of the names you come up. It is generally easier to keep it close to the original name.