In each object of a class will have its own copy of all the fields of the class. However, in certain situations, it may be required to share a common copy of fields among all the objects of the same class. This is accomplished by declaring the field(s) to be static and such fields are known as static field(s). If a field is declared static then there is one field for the entire class instead of one per object. A static field of a class is often referred to as a class variable because static field is associated with a class and not with individual instances of the class. A static field gets memory only once for the whole class no matter how many objects of a class are created. To declare a static field, prefix the field declaration in the class with the static modifier. Its syntax is,
static datatype fieldName;
int length; //length of rectangle
int breadth; //breadth of rectangle
static int rectCount =0; //count rectang objects
void setData(int l,int b)
//method to calculate area of rectangle
rectArea = length * breadth;
//class to create Rectangle objects and access static field
public static void main(String args)
//create first rectangle object
Rectangle firstRect =new Rectangle();
System.out.println("Area of Rectangle 1 : "+firstRect.area());
//create second rectangle object
Rectangle secondRect =new Rectangle();
System.out.println("Area of Rectangle 2 : "+secondRect.area());
// access static field of rectangle class
System.out.println("Total Number of Objects : "+ Rectangle.rectCount);
In the above example, the class Rectangle has two instance variables length and breadth and one class variable rectCount. When we create firstRect and secondRect objects of type Rectangle, each has its own copy of length and breadth instance variables but both share a single copy of class variable rectCount. The class variable rectCount is initialized to 0 only when the class is first loaded, not each time a new object is made. When the object firstRect is created and the setData () method is invoked, the static variable rectCount is incremented by 1 and it is set to 1 (= 0+ l). Similarly, when the object secondRect is created and the setData () method in invoked the value of rectCount variable is set to 2 (= 1+l). Finally, the statement,
System.out.println(“Total Number of Objects : “+ Rectangle.rectCount);
prints the total number of objects.
From the above example, we find that one use of class variable is to keep a count of how many objects of a class have been created in your program another use of class variable is to define constants which are shared by all the objects of the class. To understand this, let us consider a problem which maintains account information of multiple customers by updating the balance periodically with the same interest. In order to simulate it, we create a Account class which contains the fields like account number, balance, rate of interest etc. To represent individual customer, we need to create an object. Each object will store all its fields in separate memory location. As we have different account number and balance for every Account object but the rate of interest for all the account object is the same. So allocation of separate memory to data member rate of interest for all the objects will cause the following problems.
a) Wastage of memory space will occur because the same value of rate of interest is maintained by all the objects of Account class.
b) If the value of rate of interest changes over time, then each copy would have to be updated that can lead to inefficiency, wastage of time and greater potential of errors.
So in order to overcome this problem, we need to store the rate of interest only once in memory which can be shared by all the objects of the same class. This problem can be solved by declaring the ‘rate of interest’ as a static field. But this may lead to accidental modification as it may be accessed from outside the class and lead to undesirable effects on the efficiency and reliability of the program. In such a case, you have to use the final keyword along with static so that this field can never have its value changed.
Properties of Static Field
A static field of a class has the following characteristics:
1. There is only one copy of static field in a class which is shared among all the objects of the class.
2. A static field can be accessed before any object of a class is created, without reference to any object.
3. To make a static field constant in Java, make a variable as both static and final.
4. The static field is similar to static variable. The main difference is that a static variable is used to retain information between calls whereas static field is used to share information among multiple objects of a class.