Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
28,204 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