Class variables are also known as "static variables." They have the same
value among all instances of a class...considered "shared" variables.
Instance variables belong to specific instances of a class. "Objects"
are what we call instances of a class. Look at the example below. The
variables and methods marked "static" belong to all Martians. The others
are unique to each individual Martian (alice and marvin).
class Martian {
private static int martianCount = 0;
private String name;
public Martian(String martianName) {
martianCount++;
name = martianName;
}
public static int getMartianCount() {
return martianCount;
}
}
Martian alice = new Martian("Alice D. Martian");
Martian marvin = new Martian("Marvin T. Martian");