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

I still can't understand what STATIC means...

Ok, so I've watched the video a few times, tried researching it, but still can't understand what STATIC does. I know that it means that it cannot be changed, but why not just use FINAL instead of STATIC?

2 Answers

This touches on memory management. If you don't use the "static" keyword, every instance will have its own copy of the MAX_PEZ variable (here it is an instance variable). When you use "static" it becomes a class variable. Every instance of the class shares that single copy of the variable. Not a big issue when you're only creating a few instances, but imagine that you are creating 100,000,000 Pez dispensers. Only having one copy of MAX_PEZ would consume much less memory than the 100 million copies.

Oh okay, now I got it. Didn't get any of that from the explanation on the video xD. Thanks Russel.

They accomplish two different tasks. A very basic answer would be that "final" prevents change. In the context of the video, "static" is stating that the variable (here a constant) applies to ALL instances of the class. EVERY Pez dispenser you could/would ever create (instantiate) will have that MAX_PEZ=12. Using "final" just means that you also can't change the value.

But if I use

public final int MAX_PEZ = 12;

Won't it have the same effect?