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 Constructors

Caspar Claessen
Caspar Claessen
9,060 Points

pezdispenser dispenser

Dear fellow students,

I have the following question Why is the object (is it called an obejct?) where we need to pass a name to called 'Pezdispenser dispenser' and not just Pezdispenser

Anders Björkland
Anders Björkland
7,481 Points

Hi Caspar,

Excellent question! I answered below, but if it isn't crystal clear, it's alright. You will return to this question many times, and get a better understanding everytime. But if you have more questions, please ask them. Asking about what an object is and is not is touching on what's fundamental to Java and it is only good to get a better understanding of it. I myself enjoy trying to answer questions, because often I have to look up if I'm on the right track when I try to explain something - so I learn something in the process too.

2 Answers

Anders Björkland
Anders Björkland
7,481 Points

Think of the class Pezdispenser (where the code is), as a blueprint of a house. It is not able to house a family while it is just a blueprint, so you get a construction company to build the house according to the blueprint. The constructed house is now an object, and can do what it is supposed to do. A family can now move in there. Then another family wants to move in to a house. They can't move in to the already created one, a family is already living there! So they call the construction company and builds a new house.

This is also what's going on with the Pezdispenser. It's a class you have to create different pezdispensers, because maybe just one isn't enough. When you type Pezdispenser dispenser, you are giving it a reference name, a way to know which of the pezdispensers you are actually talking about. For instance, one may be called casparsDispenser, and another anderssDispenser.

To break it down. This is how you could construct a Pezdispenser:
Pezdispenser dispenser = new Pezdispenser("Yoda");
_____^_________ ^ _____ ^ _________ ^ __________________
(type/class) ___ (name) (new) (the types construction company, and your personal touch (Yoda))

You can't call it simply Pezdispenser because there may be a multitude of different objects of that class. You have an object already and you named it dispenser. There are cases where there are not expected to be a multitude of objects of a class (this is also called instances of that class), so in those cases you dont have to write something like Pezdispenser dispenser because you don't need to distinguish it from other objects.

Hm, did any of that make sense? Good luck!

Caspar Claessen
Caspar Claessen
9,060 Points

Really appreciate it that you took the time to answer my question. Your answer helps me to understand the concepts better. It is starting to "click".

Thanks Anders!

Rafał Sobczyk
Rafał Sobczyk
3,274 Points

Just as you write "String name" instead of just "String", or "int age" instead of just "int", you need to name your new Pezdispenser object.