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

I really don't understand the second line!

PezDispenser dispenser = new PezDispenser();

what is this please explain PezDispenser is the another java file? how was this automatically recognized?

dispenser??

new??

PezDispenser()??

should i worry i don't understand this line?

2 Answers

So the format for the second line is (in my own words): <Declare variable type> <name of variable> = <action>

Declaring the variable type is what we've been doing when making a new string. We would start the line with: String nameOfString = xxxx

In this case, the variable type we are using here is "PezDispenser" which we then name "dispenser."

The action is to create the new class, so "new PezDispenser();"

At this point it's calling on the file PezDispenser.java that we made in order to create the new class PezDispenser.

I know I'm answering this months after the question was asked, but maybe it will help anyone else who found this confusing.

in this line i create an object of type PezDispenser. when u create an object u have access to all his methods.