Bob P. answered 01/27/19
Duke Univ. alumnus, very experienced Java tutor, father of 4
Great question.
'STATIC' is one of the most misunderstood topics in Java.
When I ask students what they think "static" means in Java, the most common answer that I get back is they think "static" means that the variable "can't be changed". But that's incorrect... static variables can be changed. There is a different word in Java which means that something "can't be changed"... and that word is "final". Constants are usually both static and final, but those two Java keywords have different meanings and purposes.
Imagine a cookie cutter that has the shape of a gingerbread man, and you have a surface in front of you covered in cookie dough. Every time you press the cookie cutter down on the dough, you create a new instance of a cookie. You choose to put an M&M as a belly button on each cookie. Each instance of the cookie has an instance variable (that M&M).
If I ask you, "What color is the belly button?", you might respond by asking, "To which cookie are you referring?", because the answer to that question depends upon which cookie I'm referring to. This cookie over here has a blue belly button M&M, while that cookie over there has a red belly button M&M. If I make 120 cookies, there are 120 belly button instance variables, one per cookie.
Now imagine that I decide to put a counter in the cookie cutter itself and I increment the counter every time I press the cutter down to create a new instance of a cookie. If the counter were initialized to 0, then after I've created 120 cookie instances and incremented the counter each time, the counter should read 120. If I ask you, "What is the counter value?", you would have only one place to go to find that answer... you would look in the cookie cutter itself (and not any individual cookie) to see what the current value of the counter is.
In this example, the cookie cutter represents the CLASS DEFINITION. The class definition has within it an INSTANCE VARIABLE (perhaps of type String) to store the color of the belly button, and each instance of the cookie has its own copy of that variable (so each cookie instance can store a different value for that instance variable).
But the class definition also has a STATIC VARIABLE, of type int, to store the count. There is only one of those variables, even if I create 120 cookie instances. The static variable lives in the cookie cutter (the class itself) and not in any of the cookies (instances of the class).
You reference an instance variable by putting a period after an object reference to a particular instance of a cookie.
You reference a static variable (also sometimes called a "class variable") by putting a period after the class name itself.
For example:
Cookie c1 = new Cookie("red"); // creates a new cookie with a red belly button M&M
Cookie c2 = new Cookie("blue"); // creates a new cookie with a blue belly button M&M
Cookie c3 = new Cookie("brown"); // creates a new cookie with a brown belly button M&M
If the Cookie class has a getter/accessor method for belly button, and I wanted to find the color of a belly button, it depends upon which cookie I ask:
c1.getBellyButton() would return "red"
c2.getBellyButton() would return "blue"
c3.getBellyButton() would return "brown"
Assuming I made the static int counter variable publicly visible, if I want to find out how many cookies I have created, I would go directly to the class itself to find the answer:
Cookie.counter would be 3

Bob P.
12/28/21
Thomas L.
Awesome illustration, thank you. I have a better understanding of the concept.07/31/19