Static Keyword and Class Methods
Static Members
Static members let you describe information and behaviour that belong to a class as a whole. A printing lab, for example, might need one total for every printer it operates. This lesson uses Java, where the static keyword marks class-level fields and methods. In this Java context, a class method means a static method.
One owner for shared state
Static members are fields or methods associated with the class rather than with an individual object. A field declared static has one shared value for that class in the programs considered here. Creating another object does not create another copy of that field.
Consider this class:
class LabPrinter {
static int pagesPrinted = 0;
void print(int pages) {
pagesPrinted += pages;
}
static int total() {
return pagesPrinted;
}
}The = 0 sets the starting value when Java initializes the class. It does not run again for each new printer object. Whenever a printer records a print job, it changes the same pagesPrinted field.
For a job containing p pages, the shared count follows:
Here, P is the total number of pages recorded across all printers. The equation describes a change to the shared value, regardless of which printer performs the job.
Calling behaviour through the class
A static method is a method that can be called through its class without selecting a particular object. For example, LabPrinter.total() asks for the shared total. You can make this call even before creating a printer object.
An instance method has a receiver, such as printerEast in printerEast.print(6). A static method has no current receiver, so it cannot use this or directly access an instance field without an object reference.
This restriction does not prevent a static method from using objects. It can receive an object as a parameter, then access that object's permitted fields or call its instance methods through the reference. The important question is whether the code identifies the object it needs.
Feature | Static member | Instance member |
|---|---|---|
Field ownership | Shared at the class level | Separate state for each object |
Usual method call | LabPrinter.total() | printerEast.print(6) |
Current object inside a method | No this reference | this refers to the receiver |
Java permits some static accesses through an object expression, but writing the class name makes ownership clearer. Use LabPrinter.total() when you want the class-level operation.
Reading static code reliably
Example: Two printer objects record jobs in the same program:
LabPrinter printerEast = new LabPrinter();
LabPrinter printerWest = new LabPrinter();
printerEast.print(6);
printerWest.print(9);
int recorded = LabPrinter.total();Starting from zero, the first job makes the shared count 6 pages. The second makes it 6+9=15 pages. Therefore, recorded receives 15. It does not receive only the west printer's 9 pages, because the field is shared.
Also distinguish changing a field from calculating a return value. The expression return pagesPrinted + 1; would return a calculated number without updating pagesPrinted. An assignment or another modifying operation is needed to change the field.
When tracing code, keep one running entry for each static field. Update that entry only when an executed statement changes it. Creating a new object does not restart the shared counter, and merely returning a calculation does not store it back.
What to remember
- Static fields hold class-level state shared across objects.
- Call static methods through the class name to make their ownership clear.
- A static method needs an explicit object reference to access instance data.
- Track assignments separately from returned calculations.
Example: Tracking parcels across two desks
A delivery depot uses a class-level counter, one count shared by all its desk objects. The following Java class records accepted parcels and calculates how many more would fit within a stated limit. Assume a fresh execution with sequential calls and no other changes to the counter.
class ParcelCounter {
static int accepted = 0;
static void add(int count) {
accepted += count;
}
void acceptOne() {
accepted++;
}
static int remaining(int limit) {
return limit - accepted;
}
}Example: Two desks begin their shift. The statements below execute in order inside main:
ParcelCounter east = new ParcelCounter();
ParcelCounter west = new ParcelCounter();
ParcelCounter.add(4);
ParcelCounter.add(7);a) What value does ParcelCounter.accepted hold after these statements?
Both objects use the same static field. Creating the desks does not create two separate accepted-parcel counts or rerun the field's initialization for each object.
The first addition changes the shared count from zero to ________:
The second addition uses that updated shared value:
After the statements, ParcelCounter.accepted holds 11 parcels.
b) The following statements now execute. What value is stored in spaces, and does calculating it change the accepted count?
east.acceptOne();
west.acceptOne();
int spaces = ParcelCounter.remaining(20);The east desk's instance method increments the shared static field. The accepted count becomes ________:
The west desk's call increments that same field, producing ________:
The class-level query then subtracts the accepted count from the stated limit:
The variable spaces holds 7, representing room for seven more parcels. The accepted count remains 13 parcels because returning a calculation does not assign it to the shared field. The query contains no operation that changes accepted.
A quiz application uses the following Java class. Complete the statements to explain how its static method behaves across successive calls.
public class QuizScore {
public static int scoreFor(int correct) {
int bonus = 2;
bonus += correct;
return bonus;
}
}The application executes these statements in order:
int first = QuizScore.scoreFor(3);
int second = QuizScore.scoreFor(7);When tracing the second call, begin with bonus equal to . Choose local, instance, or class to complete the next statement: each invocation creates its own variable bonus. The modifier allows this method to be called through the class name without creating a QuizScore object.
A museum uses two Java console objects to record visitors arriving in groups of three. In a fresh execution, the following class starts with two visitors already recorded:
class TourLog {
static int visits = 2;
void recordGroup() {
visits += 3;
}
static void clear() {
visits = 0;
}
}The following statements then execute in order inside main:
TourLog east = new TourLog();
TourLog west = new TourLog();
east.recordGroup();
west.recordGroup();
TourLog.clear();
west.recordGroup();Complete the shared visitor count at each checkpoint. Enter exact whole numbers of visitors.
| Checkpoint | Shared visitor count |
| After east.recordGroup() | |
| After the first west.recordGroup() | |
| After TourLog.clear() | |
| After the final west.recordGroup() |