Sub topic 3
Static Methods: Behavior Without an Object
Prerequisites and the basic idea
This chapter assumes you can define classes, create objects with new, call instance methods, use public and private, and understand basic inheritance and overriding.
A static method is a method that belongs to a class rather than to a particular object. Java creates one class-level method. You can call it without first creating an instance.
An instance method needs an object because it usually reads or changes that object's fields:
account.deposit(50);A static method does not need an object. Call it through the class name:
Integer.parseInt("42");
Math.max(8, 13);The main method is static for the same reason. The Java runtime must be able to start your program before it has created any objects.
Declaring and calling static methods
Use the static modifier in the method declaration:
public class Temperature {
public static double celsiusFromFahrenheit(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
public static void main(String[] args) {
double celsius = Temperature.celsiusFromFahrenheit(212);
System.out.println(celsius);
}
}celsiusFromFahrenheit needs only its argument. It does not depend on an individual Temperature object, so making it static expresses that design.
A static method can call another static method in the same class directly. You can also call it through the class name, which often makes the ownership clearer.
Static context restrictions
A static method has no this reference. The keyword this means “the current object,” but a static method may run when no object exists.
Therefore, a static method cannot directly read an instance field or call an instance method. It can use its parameters, local variables, static fields, and other static methods.
public class Person {
private String name;
private static int count;
public Person(String name) {
this.name = name;
count++;
}
public static void printCount() {
System.out.println(count);
// System.out.println(name); // Does not compile
// this.printName(); // Does not compile
}
public void printName() {
System.out.println(name);
}
}To use instance state, a static method needs an object reference as an argument:
public static void printName(Person person) {
person.printName();
}This rule prevents ambiguity. If five Person objects exist, Java cannot guess which name a static method should use.
Static methods and inheritance
Instance methods can be overridden. Java chooses an overridden instance method using the object's runtime type.
Static methods are different. A subclass can declare a static method with the same signature, but this is called method hiding, not overriding. Java chooses the method from the declared reference type.
class Animal {
static String kind() {
return "Animal";
}
}
class Dog extends Animal {
static String kind() {
return "Dog";
}
}
public class HidingDemo {
public static void main(String[] args) {
Animal animal = new Dog();
System.out.println(animal.kind()); // Animal
System.out.println(Dog.kind()); // Dog
}
}Use static methods for operations whose result does not depend on an object's identity or state. If behavior should vary according to the actual object, use an instance method instead.
Choosing between static and instance methods
| Question | Static method | Instance method |
|---|---|---|
| Needs an object? | No | Yes |
| Can use `this`? | No | Yes |
| Typical input | Parameters and static state | Object fields and parameters |
| Dispatch in inheritance | Hidden | Overridden dynamically |
Exercise
Why does Person.printCount() work without a Person object, while a method that prints name needs an object?
Show answer
count is static and belongs to the class. name is an instance field, so Java needs a particular Person object to identify which name to print.