Java Class Methods
›Short versionOpen if you only have a minute.
- An instance method has a receiver. account.deposit(20) runs on that account, and this names it inside the method.
- A static method has no receiver. It can touch static state. It sees an instance only when you pass one in.
- A signature is the method name plus the parameter types. Overloads share a name and differ in that list.
- Overload choice is static dispatch: the compiler picks the method from the argument types and then the call is fixed.
- A static method is also statically dispatched. A subclass method with the same signature hides it. It does not override it.
- A command changes the object. A query returns information and leaves the object alone. Callers can read the name and know which they are getting.
The teller does not shout "withdraw" into the room. She withdraws from one account, the one on the counter. The account in front of the method is the . Without it, the word withdraw has no balance to change.
Methods that need an object
An reads and updates the fields of its receiver. The call writes the object first, then the method.
public class Account {
private int balance;
public Account(int opening) {
balance = opening;
}
public void deposit(int amount) {
balance += amount;
}
public int balance() {
return balance;
}
}savings.deposit(20) adds 20 to that object. checking.deposit(20) is a different call on a different receiver, even though the is the same. balance() is a query: it returns the number and leaves the fields as they were. deposit is a command: its job is the change.
Methods that do not
A is useful when the operation belongs to the type as a whole. Parsing a string into an account does not start with an account. It produces one.
public static Account parse(String raw) {
int opening = Integer.parseInt(raw.trim());
return new Account(opening);
}Call it as Account.parse("80"). Inside parse there is no this, so balance is not in scope. The method can call new, use static fields, and use its parameters.
If a subclass declares static Account parse(String) as well, that declaration hides the superclass method. The call Account.parse("80") still runs the Account version. Which class name you wrote is the method you get. That binding is .
Same name, chosen at compile time
An gives one name to several parameter lists. The compiler looks at the arguments and commits to one signature. The runtime object does not get a second vote.
public void deposit(int amount) {
balance += amount;
}
public void deposit(int amount, String memo) {
deposit(amount);
record(memo);
}
deposit(20); // deposit(int)
deposit(20, "payroll"); // deposit(int, String)That choice is also static dispatch. If you later pass an Integer where several overloads could apply, the compiler follows its conversion rules and may surprise you. Distinct names are kinder when the overloads would be easy to mix up. Overriding is a different mechanism: the signature matches a superclass method, and the body is chosen from the runtime class.
$ javac Account.java$ java Account
Account a = new SavingsAccount(80); and both classes declare static void label(). Which body runs for a.label()?
Account.label runs. The variable's compile-time type is Account, and a static call is bound to that type. SavingsAccount.label hides the method for calls written on the subclass name. It does not override the superclass method, so the runtime class of a is not consulted.