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 Harnessing the Power of Objects Constants

MAX_PEZ

I don't understand why PezDispenser.MAX_PEZ is written instead of only MAX_PEZ in Example.java, when it is not a function

3 Answers

Hello rehan guddu,

When calling something (like a method...) we have to show where each method is coming from.

PezDispenser.MAX_PEZ is written instead of only MAX_PEZ

Because PezDispenser.java is the Class (or 'home') of the MAX_PEZ.

We are basically saying, "You know that thing in PezDispenser, named MAX_PEZ? Can you get it for me?"

Please let me know if there is anything else I can help with.

Thank you. I understood the concept

Hi Rehan, MAX_PEZ is a class field/member, so it resides in the PezDispenser universe and therefore it needs the dot notation and the class name(or an instance of a class if it is not a static field). As with class fields, instance fields are a part of the instance itself. You cannot invoke them without their instance. They belong to the instance, not to the global scope(In our case the main method which we run our program).