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

johnny lawma
johnny lawma
3,298 Points

java object oriented program (OOP) kind of confuse me on classes

    System.out.println("We are making a new PEZ Dispenser");
    PezDispenser dispenser = new PezDispenser();
    System.out.printf("The dispenser is %s %n", dispenser.chracterName);

so far as i know, the whole concept OOP is to create a blueprint, use that blueprint as template or classes and add or change something on the blueprint. CORRECT ME IF I'M WRONG.

so the code, why the we create "-- new pexDispenser(); --" on the second line?. the second once is the third line on the code, "-- dispenser.chracterName --" why do we include " -- dispenser -- " if we just filling out the chracter name. why?

This once is Really confusing :(

1 Answer

alastair cooper
alastair cooper
30,617 Points

There is no global variable called characterName to use. Each instance of the PezDispenser class has it's own member variable called characterName. So to get the character name you have to create an instance of the class and call the characterName getter (unless you make the getter 'static' then you won't need an instance. However, this doesn't make sense, because something can't have a name if it doesn't exist)

Hope this helps