Search code examples
javaoopinstance-variablesclass-variables

When will I use a Class Variable in Java v.s an Instance Variable?


Here is a generic class that I have defined, what I would like to know is when I am creating more specific classes for instance a CAR class when would I use a Class Variable? My personal understanding of a class variable is that a single copy of a class variable that has been declared in a class will be declared using the keyword static, and that each object that has been instantiated from the class will contain a single copy of the class variable.

An instance variable allows each instance of a class / object that has been created from the class to have a separate copy of the instance variable per object?

So an instance variable is useful for defining the properties of a class / data-type e.g a House would have a location, but now when would I use a class variable in a House object? or in other words what is the correct use of a class object in designing a class?

public class InstanceVaribale {
public int id; //Instance Variable: each object of this class will have a seperate copy of this variable that will exist during the life cycle of the object.
static int count = 0; //Class Variable: each object of this class will contain a single copy of this variable which has the same value unless mutated during the lifecycle of the objects.

InstanceVaribale() {
    count++;

}
public static void main(String[] args) {

    InstanceVaribale A = new InstanceVaribale();
    System.out.println(A.count);
    InstanceVaribale B = new InstanceVaribale();
    System.out.println(B.count);
    System.out.println(A.id);
    System.out.println(A.count);
    System.out.println(B.id);
    System.out.println(B.count);    
    InstanceVaribale C = new InstanceVaribale();
    System.out.println(C.count);
}
}

Solution

  • My personal understanding of a class variable is that a single copy of a class variable that has been declared in a class will be declared using the keyword static, and that each object that has been instantiated from the class will contain a single copy of the class variable.

    No. It's not that "each object will contain a single copy". A static variable is associated with the type rather than each instance of the type. The instances don't have the variable at all.

    There's exactly one variable (assuming you're only loading it from one classloader) however many instances of the type there are. No instances? Still one variable. A million instances? Still one variable.

    Static variables are mostly useful for constants or constant-alikes - things like loggers, or "the set of valid prices" etc. Things which don't change over the course of the application. They should almost always be final in my experience, and the type should be an immutable type (like String). Where possible, use immutable collections too for static variables - or make sure the variable is private and that you never mutate the collection within the class.

    You should avoid using static variables to store global changing state. It makes code much harder to test and reason about.