|
Member variables store the object’s state. But static variables are special type of variables that are not associated with an object, they are associated with the type of object. In other words, the static variables (aka class variables) are associated with a class of objects rather than an object itself.
In order to understand static variables, you must understand why you need them in the first place. Let us say you want to keep a count of the number of instances created from a particular class. How will you do that? You may declare a variable something like count and increment it each time an instance is created (probably in the constructor). But it does not work. Since count is an instance variable, it will be initialized each time a new instance is created. So it will not help in tracking how many instances are created. You need to have a variable, which is not initialized with each instance creation. The solution is to declare count as static (We will soon see how to do that). The static count variable has only one copy shared among the class and all its instances. It is initialized only once when the class is loaded[l4]. So we can increment it with each instance creation.
|
|
Since static variables are associated with a class, you can access them without creating even a single instance of a class. |
You can declare a static variable by simply applying the static modifier in its declaration. You can access such static variable either by referring it using the class’s name or by referring it using any of its instances. Let us declare a simple class Parrot with the static variable, population. Listing 3.10 illustrates how you can use it to keep track of all the Parrot instances that are being created.

Note that static variables come into life when the class is loaded and initialized only at that time. For example, population is initialized only once when the Parrot class is loaded. After that, all the instances of Parrot and the Parrot class itself are sharing the same copy of population. Since it is shared, it can be referenced using either class name or the instance of Parrot class, for example tweety.population. Figure 3.5 shows how the Parrot class and all its instances actually refer to the common copy of static variable population.
![]() |
Figure 3.5 Various ways of accessing static variable population of Parrot class
Whenever the Parrot class or any of its instances access the static variable population and changes its value, this common copy is changed. Therefore, both Parrot.population and tweety.population have the same value (which in this example is 2) in listing 3.10.
We just saw that the static variable can be accessed by referring to the class as well as any of its instances. However, it is not a good coding practice to access the static variable via instances. This is especially true while modifying the static variable. Listing 3.11 illustrates how modifying the static variable using instances may lead to the code that is confusing to read. In this example, the static variable family is being accessed via Parrot instances, sweetie and tweety.

Looking at the code of Tester, it appears that we are modifying the parrot family for tweety. But the common copy is modified because family is a static variable. This code works just fine but may be confusing to read. It might give you a false impression that both sweetie and tweety have their own copies of the variable family. To avoid this kind of confusion, it is recommended to always access static variables either via class name or via static methods. For example, you should refer the static variable family in listing 3.11 as Parrot.family.
|
|
You may find exam questions that purposefully confuse you by accessing the static variable via instances. While answering questions related to static variables, always remember that static variable has only one copy that is shared among all instances and the class itself. |