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 (Retired) Harnessing the Power of Objects Methods and Constants

Jae Kim
Jae Kim
3,044 Points

What does Craig mean by creating an instance of a class?

I understand that this operation PezDispenser.MAX_PEZ will not work in the instance we do not use the "static" keyword on the PezDispenser class itself.

Does he mean that the "static" keyword allows us to have the PezDispenser class have access to the field public final int MAX_PEZ = 12;?

What would the code look like if we were to not use the "static" keyword in order to set the MAX_PEZ count of the dispenser to 12? What would it be like creating an instance of the PezDispenser class in order to obtain it's dispenser count?

I apologize about my inquiries and would greatly appreciate any advice as I am re-learning Java OOP in order to obtain a better grasp at it :).

1 Answer

You are exactly right! So basically what the "static" keyword means, is that the value is assigned directly to the class, instead of being assigned every time a new one is created. This means that no matter what, every single PezDispenser has a MAX_VALUE of 12, and that's just automatic. What this does is make it so anyone can find out this value without having to go through the trouble of creating a new one just to get their answer. So instead of having to ask "How much is the max amount that can fit in a 'Yoda' PEZ Dispenser?" you can just ask what the max amount is in a PEZ Dispenser in general.

Another example:

public class CerealBox(String typeOfCereal) {
   public static final int ouncesOfCereal = 12;

In this example you can call "CerealBox.ouncesOfCereal" and it will tell you "12". However, if that static keyword wasn't there, you would have to go about it this way:

public class CerealBox(String typeOfCereal) {
   public final int ouncesOfCereal = 12;

CerealBox cheerios = new CerealBox(cheerios);

and then you would have to call "cheerios.ouncesOfCereal" to get your answer. (Obviously both of these are missing a few crucial constructor lines, but I cut out the pork to get to the important stuff).

Hope all of that makes sense!

Jae Kim
Jae Kim
3,044 Points

The code that you wrote definitely cleared it up for me! Thank you.

From what I gather, the static keyword is also "class-specific" in which the field itself could not be used in other classes correct?

So I wouldn't be able to do something along the lines of


In this case, this would cause an error in Java correct?

Jae Kim I don't believe so... I think what that means is just that it assigns a value to the class. Any time you create an instance of a class (a new cereal box, if you will), it's still a cereal box, so it's still going to have that value. I just tried a few things playing around with the static keyword, and I didn't get any error messages after creating a new one.

Side note: Craig mentions common practice for final/static values is to make it all uppercase with underscores. So technically, it should be something more along the lines of:

public static final int OZ_OF_CEREAL = 12;

but, that's just common practice. Doesn't actually change anything front-end-wise.