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

Jun Dong
Jun Dong
4,754 Points

PezDispenser pd = new PezDispenser("Yoda"); pd.MAX_PEZ

Why did he have to make the pd object to access MAX_PEZ?

1 Answer

Steven Parker
Steven Parker
231,184 Points

When first created, MAX_PEZ is an instance value, so you must create an object to access it.

But if you keep going in the same video, it is changed to a class value by adding "static" to the declaration. At that point, you no longer need to create an object and you can access the value directly using the class name.

Jun Dong
Jun Dong
4,754 Points

Thanks, but can you also explain to me what exactly the Static keyword do?

Steven Parker
Steven Parker
231,184 Points

The keyword "static" associates it with the class instead of an instance. So instead of a new one one being created with every new object, all objects share the same one. And as I mentioned before, it can be accessed using the class name when no object has been created.