So far, we have not looked at the BouncingBall class. If you are interested in really under- standing how this animation works, you may want to study this class as well. It is reasonably simple. The only method that takes some effort to understand is move, where the ball changes its position to the next position in its path.
We shall leave it largely to the reader to study this method, except for one detail that we want to discuss here. We start with an exercise.
Exercise 5.67 In class BouncingBall, you will find a definition of gravity (a simple inte- ger). Increase or decrease the gravity value; compile and run the bouncing ball demo again. Do you observe a change?
5.13 Class variables and constants | 191
The most interesting detail in this class is the line private static final int GRAVITY = 3;
This is a construct we have not seen yet. This one line, in fact, introduces two new keywords, which are used together: static and final.
5.13.1 The static keyword
The keyword static is Java’s syntax to define class variables. Class variables are fields that are stored in a class itself, not in an object. This makes them fundamentally different from instance variables (the fields we have dealt with so far). Consider this segment of code (a part of the BouncingBall class):
public class BouncingBall {
// Effect of gravity.
private static final int GRAVITY = 3;
private int xPosition;
private int yPosition;
Other fields and method omitted.
}
Now imagine that we create three BouncingBall instances. The resulting situation is shown in Figure 5.8.
Concept:
Classes can have fields. These are known as class variables or static variables. Exactly one copy exists of a class variable at all times, inde- pendent of the number of created instances.
\3RVLWLRQ [3RVLWLRQ
EDOO%RXQFLQJ%DOO
\3RVLWLRQ [3RVLWLRQ
EDOO%RXQFLQJ%DOO
\3RVLWLRQ
[3RVLWLRQ
EDOO%RXQFLQJ%DOO
*5$9,7<
BouncingBall
LVLQVWDQFHRI
LVLQVWDQFHRI LVLQVWDQFHRI
Figure 5.8 Instance variables and a class variable
As we can see from the diagram, the instance variables (xPosition and yPosition) are stored in each object. Because we have created three objects, we have three independent copies of these variables.
The class variable GRAVITY, on the other hand, is stored in the class itself. As a result, there is always exactly one copy of this variable, independent of the number of created instances.
Source code in the class can access (read and set) this kind of variable just as it can an instance variable. The class variable can be accessed from any of the class’s instances. As a result, the objects share this variable.
Class variables are frequently used if a value should always be the same for all instances of a class. Instead of storing one copy of the same value in each object, which would be a waste of space and might be hard to coordinate, a single value can be shared among all instances.
Java also supports class methods (also known as static methods), which are methods that belong to a class. We shall discuss those later.
5.13.2 Constants
One frequent use for the static keyword is to define constants. Constants are similar to vari- ables, but they cannot change their value during the execution of an application. In Java, con- stants are defined with the keyword final. For example:
private final int SIZE = 10;
Here, we define a constant named SIZE with the value 10. We notice that constant declarations look similar to field declarations, with two differences:
■ they include the keyword final before the type name; and
■ they must be initialized with a value at the point of declaration.
If a value is intended not to change, it is a good idea to declare it final. This ensures that it cannot accidentally be changed later. Any attempt to change a constant field will result in a compile-time error message. Constants are, by convention, often written in capital letters. We will follow that convention in this book.
In practice, it is frequently the case that constants apply to all instances of a class. In this situa- tion, we declare class constants. Class constants are constant class fields. They are declared by using a combination of the static and final keywords. For example:
private static final int SIZE = 10;
The definition of GRAVITY from our bouncing-ball project is another example of such a constant. This is the style in which constants are defined most of the time. Instance-specific constants are much less frequently used.
We have encountered two more examples of constants in the scribble project. The first example was two constants used in the Pen class to define the size of the “random squiggle” (go back to the project and find them!). The second example was the use of the color constants in that project, such as Color.RED. In that case, we did not define the constants, but instead used con- stants defined in another class.