Accessing
Static Fields through Object References
In
the previous post, we accessed the static method getCount by using the class
name.
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());
}
}
System.out.println(new
Ball().getCount());
Here, we first create an instance of Ball. The class constructor would increment the count by 1. After the object is fully constructed, the runtime calls its getCount method. Thus, the count value printed to the console also includes the currently created object.
Inheriting Static Fields
In
previous post, we learned about inheritance. The same way the class fields and
methods are inherited by a subclass, the static fields of a class will be
inherited in a subclass. Consider the following class declaration:
class
RedBall extends Ball {
}
Here,
RedBall simply inherits the Ball class, thereby inheriting all its members.
Now, if we instantiate RedBall and get the ball count by using the following statement,
we find that the count variable belonging to the Ball class is incremented
during the construction of the RedBall object:
System.out.println(new
RedBall().getCount());
This
confirms that all static fields are also inherited by the subclasses.
Creating a Truly Global Variable
According
to the best practices of object-oriented design, the fields of a class should
always be made private. Now, what would happen if we declare them public? Well,
the answer is simple.
Any
code outside the class definition would be able to access and modify these fields. Although this is considered bad design, making the static fields public allows us to create a truly global variable. For example, modify the declaration of the count field in our earlier program as follows:
public static int count = 0;
Now, we are able to access this count field anywhere in the code with the statement Ball.count. There is no need for a getter method.
According to good programming practices recommended by Sun (now Oracle), instance variables should always be declared private. The problem with public non-final variables is that they can be changed by anyone who uses the enclosing class without our knowing. Using getters and setters gives us a chance to monitor/verify any reads or writes of that data, thus leading to more reliable programs. It is okay to declare class variables public when they are used as constants and marked final.
Creating Application Constants
From the previous section, we know how to create an application global variable declaration. We also learned the use of the final keyword previously. If we make our global variable declaration final, it will create a constant that cannot be modified throughout the application.
The following statement shows how to create such a constant:
class Constants {
public final static double PI = 3.14;
}
The variable PI, which is declared static, is accessible without creating an instance of the Constants class. The variable is initialized to a value of 3.14 at the time of declaration. Because it is declared final, its value cannot be modified further in the application. Thus, we have successfully created an application constant. To use this constant in our code, we would use the syntax Constants.PI. The visibility of this constant in our program code is decided by the access modifier applied to its declaration, which in this case is public. This constant can be accessed anywhere in our program code as long as the code has access to the class Constants. The names of variables declared as class constants should be all uppercase, with words separated by an underscore character. We should avoid the use of ANSI constants in our program code as our own constants for ease of debugging.
Sushant Mishra
30-Mar-2017