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!

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

Confused, looking for verification

PezDispenser dispenser = new PezDispenser();

Let me see if i understand this correctly, please correct me if I'm wrong.

<> ' PezDispenser ' tells java what class to pull characteristics from <> ' dispenser ' tells java the name of the object <> ' = new PezDispenser(); ' tells java to create the object

My confusion lies with why wouldn't java automatically apply the characteristics of the PezDispenser class when it creates the object? Why do you have to define the class before hand?

For example, would this work?

dispenser = new PezDispenser();

PezDispenser at the start says what type of Object it is. Its kind of like saying, hey! Allocate some memory for this PezDispenser. Then you proceed to give the object its name, and you assign it to the value of a new PezDispenser. I'm not sure if you've hit constructors yet, but it essentially creates the object and its properties.

For an example.

Dog max = new Dog();

The first Dog says hey, this animal is going to be a dog, make sure there is enough space in the house for it.

The new Dog() part builds all of its legs, its tail, its nose, its tongue, etc and creates the dog.

The process will clarify as you advance in programming.

Rodrigo Gomez-Palacio
Rodrigo Gomez-Palacio
1,125 Points

I am also confused. There are too many combinations of the words 'PEZ' AND 'DISPENSER' in this video that it's kind of hard to keep track.

So to create an object, do we have to assume/know that a class exists of the same name as the object type we wish to create? At this point in our knowledge, what object types can we assume from the standard library?

In this code,

Dog max = new Dog();```

the type is Dog, the name is max, and Dog() creates the dog. Why is the naming of the actual object of its same type? Like it's hard to understand that the type is also the name of the object. But the name is actually also "max"? Also, what parameters would go in the parentheses? 


By saying new Dog() we are actually calling the Dog class' constructor. Constructors essentially initialize the object. You can use it to create new data for things such as a name and a breed, which would be accessible through things such as max.name and max.breed. If you continue along in the track, they attempt to explain it. The first Dog is its type. The max is its name, which is what you'll use to access the instance variable. The last Dog() calls the constructor which I talked about above. In the next few videos, he tries to show them to you. If you still are confused let me know. I'll write a sample class for you and I'll comment it up.

2 Answers

This video finally cleared up Java Classes for me, i'm also their videos for Parameters and Constructors as well. https://www.youtube.com/watch?v=hAU8t5DGido

I'm so glad for you!(:


Thanks, I think i'm starting to grasp it. This whole series is whopping my butt. I think, for me personally, it would help to see an step-by-step explanation of how java is processing the code after each lesson.

Brush it off! Programming is a lot of failing, but thats the only way you can be successful. You'll learn what to do right by learning what not to do. If you'd like a more advanced explanation here is a page for it. http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html