Sub topic 9
Class-level state and behavior with static
A class describes what its objects can hold and do. Each object is an instance, or one item created from that class. An instance field is a value stored separately in each object. An instance method is an action called on one object.
Sometimes, a value or action belongs to the whole class instead. In Java, the keyword static marks that kind of member. A member is a field or method defined in a class.
Shared fields and class methods
A static field is shared by the objects of a class. For example, a Visitor class could keep a count of all visitors created. Each visitor still has its own name, but there is one shared count.
A static method belongs to the class too. You can call it using the class name, without creating an object first.
| Member | Belongs to | Example |
|---|---|---|
| Instance field | Each object | One visitor’s name |
| Static field | The class | The number of visitors created |
| Instance method | An object | Get that visitor’s name |
| Static method | The class | Get the shared count |
Try a visitor counter
Save this program as Visitor.java. A constructor is the part of a class that sets up a new object. Here, each constructor call also adds one to the shared count.
public class Visitor {
private static int totalCreated = 0;
private String name;
Visitor(String name) {
this.name = name;
totalCreated++;
}
String getName() {
return name;
}
static int getTotalCreated() {
return totalCreated;
}
public static void main(String[] args) {
System.out.println("Created: " + Visitor.getTotalCreated());
Visitor first = new Visitor("Ada");
Visitor second = new Visitor("Lin");
System.out.println("First: " + first.getName());
System.out.println("Second: " + second.getName());
System.out.println("Created: " + Visitor.getTotalCreated());
}
}Compile and run it from the folder containing the file:
javac Visitor.java
java VisitorThe output is:
Created: 0
First: Ada
Second: Lin
Created: 2Here is what happens:
totalCreatedstarts at zero. Its type,int, stores whole numbers. The field isstatic, so both visitors share it.- Each call to
new Visitor(...)creates an object and runs the constructor. Thenamefield has typeString, which stores text. Each object gets its own name. - In
this.name = name,this.namemeans the new object’s field. Thenameon the right is the value passed to the constructor. totalCreated++adds one to the shared count. After two visitors are created, the count is two.first.getName()calls an instance method onfirst.Visitor.getTotalCreated()calls a static method using the class name.
The private keyword prevents code outside the class from directly changing these fields. The static getTotalCreated method provides a way to read the count.
What static methods can access
A static method has no current object. It can use static fields and call other static methods of its class directly. For example, getTotalCreated() reads totalCreated.
It cannot directly use an instance field such as name, because it has no particular visitor’s name to read. It can use an instance field or method if it has an object to work with. In the example, the static main method calls first.getName() because it has the object first.
The main method is where this program starts. It is public, so Java can access it from outside the class, and static, so Java does not need to create a visitor to call it. The word void means the method returns no value. String[] args can hold text supplied when the program starts.
Exercise
If the program creates a third visitor named "Mia", what will Visitor.getTotalCreated() return? Does that change the name stored in first?
Show answer
It returns 3. The name in first remains "Ada" because name is an instance field.