
Static Variables
Adding a couple of additional fields to the car example should help to clarify the class variable concept.
First, each car has a serialNumber (called a vehicle identification number or VIN). This is pretty simple.
Based on what you have already learned, simply add a serialNumber instance variable to the Car class.
public class Car{
String color;
String type;
int serialNumber;
... // The rest of the class goes here. This symbol (...) is used
// throughout this text to indicate that not all the code is
// is shown but only the code that is pertinent at the time.
}
This serialNumber, however, must be a unique integer for each car created.
In this case, the serialNumber should be unique for each car object created from the Car class. To make sure
each car has a unique serialNumber, you can use the Car class and a class variable to keep track of the total
number of cars created.
public class Car{
String color;
String type;
int serialNumber;
static int carCount;
...
}
Here the modifier “static” was added to the carCount variable in this class. The carCount variable is related to
the class, not any single Car object.
To access this variable, you use the name of the class and the class variable name. For example, the line of
code below sets the carCount to 1.
Car.carCount = 1;
Oddly, you can use either the class name (Car) or any object reference of that type to access class variables,
so the following code would do the same thing:
Car myCar = new Car(“black”, “Ranger”);
myCar.carCount = 1;
This makes it look like carCount is an instance variable, doesn’t it? Therefore, it is considered clearer, and
preferred, to use the class name when accessing class/static variables.
Note that the carCount variable is created and initialized not when an object is instantiated but when the class
is first loaded into the JVM by the class loader! That is because the carCount data is associated with the class
(Car) and not any single instance of the class (myCar). No matter how many Cars get created, there is still
just one carCount.
Recall that what is needed is to set the serialNumber for every Car and that number must be unique. The
carCount class variable can be used to achieve this goal. However, to pull this off, more work on the
constructors for the Car class is needed.
Car(){
carCount++;
serialNumber = carCount;
}
Car(String c, String t){
color = c;
type = t;
carCount++;
serialNumber = carCount;
}
This code increments the number of Cars (carCount) every time a Car is created and then assigns carCount to
the car object’s serialNumber. Now each Car has a unique serial number, created from the class’s carCount.
When each car is created, the constructor increases the value of carCount by one. The carCount variable
represents the total number of cars that have been created so far.
Now take a look at the code below to visualize what is happening.
Car car1, car2, car3; //create 3 object references.
car1 = new Car(“red”,”Sedan”);
car2 = new Car(“blue”, “Station Wagon”);
car3 = new Car(“white”, “Coupe”);
When the declaration of a Car reference is made (Car car1), the class loader must load the Car class into the
JVM. At that time, carCount is initialized and available. Every object type used in an application has a
respective Class object stored in a special place on the JVM’s heap. This Class object contains all of the
details about the object type:
What properties it has
What the properties' types are
What methods the class has and what arguments the methods take
What code is executed when a method is called and so forth.
Static data is stored in the Class object on the JVM’s heap.
All three cars have their own independent color, type, and serialNumber, but they share one carCount
variable.
Class variables are global (only one exists in memory per class). All instances share the one carCount, which
is obtained through the class. Each instance has its own color, type, and serialNumber. All code in the JVM
can reference static information of a class depending on the access modifier, which is covered in another
chapter.
Static Variables
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services