We have already seen the use of the static keyword in our Java programs. In fact, the first program we encountered uses the static keyword, where it was applied to the main method.
The static keyword can also be applied to the fields of a class. The only other application of the static keyword is to create initializers. We can use the static keyword in all three cases—that is, used with
· Fields
· Methods
· Initializers
The Static Fields
A class field may be marked as static. But why would you do so? Suppose we have created an application that uses several of our own classes. The application user can create several objects of these classes during runtime. Therefore, we may want to know how many objects of each type a user has created in a typical session to get an understanding of the memory usage of our application. In this case, a static field will help in maintaining the count of number of objects created.
Another good application of a static field would be to create a truly global variable. As we already know, Java does not allow us to create any declarations outside the class definition (C++ allows us to create global variable declarations). However, we are able to create a globally accessible variable within our application using the static keyword. One more useful application of a static field declaration is to create an application-level programming constant.
We will learn all these techniques in this section.
· A static field is associated with the class definition itself.
·
It is shared among all the
instances of the class.
· This means that when a class
is instantiated, no separate memory allocation will occur for a static field.
·
The memory allocation for a
static field happens only once, and that is when the class loads.
· When JVM loads the class definition into memory, it allocates space for all the static fields of a class. This memory space (that is, the fields) will be shared among all the objects of a class that are created throughout the life of the program.
Conversely, all non-static fields will have their own memory allocation in each instance of the class. Therefore, when we have more than one instance of a class, each object will have an independent memory allocation for all its non-static fields. In other words, all non-static variables are copied into the newly created instance in memory.
Thus, in the case of a non-static variable, if we modify its value for a particular instance, the changes will remain local to that object and are not reflected in other instances of the class.
However,
for a static field, because only one allocation is shared among all the
instances, any changes to it will be visible to all the instances of
the class. An important application of this would be when we want to keep a
count on how many instances of a class have been created by the running
application. Let’s look at this usage with the help of an application.
class Ball {
private static int count = 0;
public static int getCount() {
return count;
}
public Ball() {
count++;
}
}
public class BallGame {
public static void main(String[] args) { for (int i = 0; i < 10000; i++) {
int number = (int) (Math.random() * 10);
if (number == 5) {
new Ball();
}
}
System.out.println("No of balls created: " + Ball.getCount());
}
}
The
BallGame class provides the main method in which we instantiate the Ball class.
To simulate the situation that a player may create any number of balls, we use
a randomizer in the program to set the number of balls. The randomizer
generates a random number in the range 0 to 10. The program runs 10,000
iterations, creating a random number in each. When the generated random number
equals 5, a Ball is created. At the end of the loop, we print the total number
of balls created on the user console. The number of balls is retrieved using a
getter method on the count field. Note that the getter method getCount is
declared static and is called by using the syntax ClassName.methodName.
Samuel Fernandes
10-Apr-2017