Instance variables vs static variables

  • There are 2 kinds of variables that can be defined inside a class (that is outside any method):

    public class Circle
    {
        public        double radius; // (1) an instance variable
        public static int count;     // (2) a  "static" variable
        ...
    } 

  • How are instance variables and static variables of objects different:

    • Each object has its own copy of an instance variable

    • Static variable belongs to the class and all objects of that class share the same copy of a static variable

  • In other words:

    • There is only 1 copy of a static variable in a Java program !