Sub topic 4
Call Static and Instance Methods
A method is a named set of instructions that a program can call. In Java, a method can belong to a class or work with an object.
A class is a blueprint for creating objects. An object is one thing created from a class. For example, a Lamp class can describe lamps, while a desk lamp and a floor lamp are separate objects.
Two kinds of methods
A static method belongs to the class. You can call it using the class name, without first creating an object:
Lamp.explain();
An instance method works with a particular object. An instance is an object created from a class. To call an instance method, use an object:
desk.turnOn();
The dot connects the class or object to the method call.
Worked example: Two lamps
This program creates two lamps. It turns on the desk lamp, then shows the state of each lamp.
Save the code in a file named Lamp.java.
class Lamp {
boolean on = false;
static void explain() {
System.out.println("Each lamp has its own switch.");
}
void turnOn() {
on = true;
}
void showState() {
System.out.println(on);
}
public static void main(String[] args) {
Lamp.explain();
Lamp desk = new Lamp();
Lamp floor = new Lamp();
desk.turnOn();
desk.showState();
floor.showState();
}
}To run it, open a terminal in the folder containing the file. A terminal is a place where you enter commands. You need the Java Development Kit, or JDK, installed. It provides the tools to compile and run Java programs.
javac Lamp.java
java LampThe output is:
Each lamp has its own switch.
true
falseThe javac command compiles the program, which means it prepares the code to run. The java command then runs it.
Follow the calls
The explain method is static, so Lamp.explain() calls it through the class. No lamp object is needed for that call.
The expression new Lamp() creates a lamp object. The program calls it twice, so desk and floor refer to different objects.
Each lamp has an on field. A field is a variable that belongs to a class or object. Here, on belongs to each lamp object. Its type is boolean, which holds either true or false. Each lamp starts with on set to false.
The call desk.turnOn() runs turnOn for the desk lamp. Inside that method, on = true changes the desk lamp’s field. It does not change the floor lamp’s field.
The showState method reads the field of the object used in the call. That is why the desk lamp prints true and the floor lamp prints false.
The word void means these methods do not return a value to the caller. The line starting with public static void main marks where Java starts this program. For now, you can copy that line as shown.
Choose the right kind
Use an instance method when the work needs a particular object’s fields, such as turning on one lamp. Use a static method when the work does not need a particular object, such as printing the general explanation above.
A static method has no current lamp object. It cannot use on by itself. It can still work with an object if it has one: the static main method calls desk.turnOn().
An instance method can use its object’s fields directly. It can also call static methods, such as Lamp.explain().
A common error is writing Lamp.turnOn(). Java needs to know which lamp to turn on. Create or obtain a lamp object, then call the method on that object.
Exercise
In the example, what would floor.showState() print after the program also calls floor.turnOn()?
Show answer
It would print true, because that call changes the floor lamp’s own on field.
Exercise
Should a method that switches off one particular lamp be static or an instance method?
Show answer
It should be an instance method, because it needs to change the state of a particular lamp object.