Static Methods vs Instance Methods
A railway enquiry counter answers general questions: "What's the fare from Pune to Goa?" It doesn't need to know who you are. A ticket, though, answers questions about itself: "Which berth is on this ticket?" Java methods come in the same two kinds. Static methods belong to the class and answer general questions. Instance methods belong to an object and work with that object's own data.
Called on the Class vs on an Object
public class Ticket {
String name;
int berth;
static int issued = 0;
static int fareFor(int km) {
return km * 2;
}
String describe() {
return name + " in berth " + berth;
}
public static void main(String[] args) {
Ticket t1 = new Ticket();
t1.name = "Navika"; t1.berth = 23; issued++;
System.out.println("Fare for 300 km: Rs. " + Ticket.fareFor(300));
Ticket t2 = new Ticket();
t2.name = "Srihitha"; t2.berth = 24; issued++;
System.out.println(t1.describe());
System.out.println(t2.describe());
System.out.println("Tickets issued: " + issued);
}
}Fare for 300 km: Rs. 600
Navika in berth 23
Srihitha in berth 24
Tickets issued: 2How it works
fareFor(lines 6 to 8) is static: it uses only its parameter, not any ticket's data. Line 17 calls it on the class:Ticket.fareFor(300).describe(lines 10 to 12) has nostatic, so it's an instance method. It readsnameandberth, which belong to a particular ticket.t1.describe()uses t1's fields;t2.describe()uses t2's. Same method, different objects, different results.issuedis a static field: one counter shared by the whole class.
Try it yourself: add an instance method boolean isLowerBerth() that returns true when berth % 8 is 1 or 4, and call it on both tickets.
What Each Can Access
| Static method | Instance method | |
|---|---|---|
| Called as | Ticket.fareFor(300) | t1.describe() |
| Needs an object first | No | Yes |
| Can use static fields and methods | Yes | Yes |
| Can use instance fields and methods directly | No | Yes |
Has this | No | Yes |
A static method has no object, so it has no name or berth to read. Trying gives one of the most common beginner errors:
S.java:4: error: non-static variable count cannot be referenced from a static context
count++;
^That's why every method called directly from main in this course has been static: main itself is static. The fix is either to make the method static (if it doesn't need object data) or to create an object and call the method on it.
When to Use Which
| Make it static when | Make it an instance method when |
|---|---|
It works only with its parameters: Math.max(a, b) | It reads or changes an object's fields: ticket.describe() |
It's a utility or helper: Integer.parseInt(text) | Its answer depends on which object you ask |
| It doesn't need any object state | It models a behavior of the object |
Common Mistakes
Calling an instance method from main without an object. Create the object first: Ticket t = new Ticket(); t.describe();.
Calling a static method through an object. t1.fareFor(300) compiles, but misleads readers into thinking it uses t1's data. Write Ticket.fareFor(300).
Using static fields for per-object data. A static name would be shared by every ticket; each new name would overwrite the last.
Interview Corner
What is the difference between static and instance methods?
A static method belongs to the class; it's called as ClassName.method(), needs no object and can't use instance fields or this directly. An instance method belongs to an object; it's called on that object and can use the object's fields as well as static members.
Why can't a static method access instance variables directly?
Because it runs without any object. Instance variables exist only inside objects, so there's no particular object whose variable it could use. It can access them through an object reference, such as t1.name.
Practice
- Easy: Write a static method
kmToMiles(double km)and call it frommainwithout creating any object. - Harder: Add an instance field
int kmtoTicketand an instance methodfare()that calls the staticfareFor(km). Print each ticket's fare.
Key Takeaways
Was this tutorial helpful?