TOPIC 3.7 · STUDY
Class Variables and Methods
A static variable or method belongs to the class, not to one object. A class method may use static data and call other static methods, but it cannot touch an instance variable unless an object is supplied. A static variable is shared by every object of the class, and callers reach a public one as ClassName.name.
3.7.A
Class methods and static access
The keyword static marks a variable or method as belonging to the class itself. It is not stored inside one constructed object, and a static method call is not aimed at one particular instance. From another class, write ClassName.method() with the class name first.
A static method may read or update static variables and may call other static methods in the same class. It cannot name an instance variable such as brightness on its own. That use is a compile-time error, even if objects of the class will exist later. To work with object data, pass an object as a parameter and call through that reference.
public class Signal
{
private int brightness;
private static int spare = 3;
public Signal(int brightness)
{
this.brightness = brightness;
}
public int getBrightness()
{
return brightness;
}
public static int pad()
{
return 2;
}
public static int total()
{
return spare + pad();
}
}
System.out.print(Signal.total());
total is static, so it may use the class variable spare and call the class method pad. The printed result is 5. The call is written Signal.total() from outside Signal. If total tried to write brightness++ with no Signal object in hand, the code would not compile.
// does not compile: no object supplies brightness
public static void flash()
{
brightness++;
}
// compiles: the parameter supplies the object
public static int peek(Signal s)
{
return s.getBrightness();
}
Key points
- static means the member belongs to the class, not to one object.
- A static method may use static variables and call other static methods.
- Using an instance variable in a static method with no object is a compile-time error.
- From another class, call ClassName.method(), such as Signal.total().
A class method has no instance variable unless an object is supplied.
3.7.B
Shared class variables
A static variable is shared by every object of the class. There is one copy for the class, not a separate copy inside each object. Constructors and methods can update that shared value, so building several objects can change the same count everyone sees.
public class GatePass
{
public static int issued = 0;
private int code;
public GatePass(int code)
{
this.code = code;
issued++;
}
public int getCode()
{
return code;
}
}
GatePass first = new GatePass(11);
GatePass second = new GatePass(17);
System.out.print(GatePass.issued);
issued starts at 0. Each constructor adds 1, so after two GatePass objects the shared value is 2. first and second each keep their own code, but both see the same issued. From another class, a public static variable is reached with the class name: System.out.print(GatePass.issued);
The shared count lives with the class. Object A and object B do not each keep a separate copy of that count.
Key points
- A static variable is shared by every object of the class.
- Constructing objects can update that shared value.
- Each object still has its own instance variables.
- From another class, read a public static variable as ClassName.name.
Changing a static variable through one constructor or call changes the single shared copy. Every object of that class sees the updated value.