Static Methods vs Instance Methods

Naveen AnamaneniUpdated · 5 min read

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

Static: ClassName.method(). Instance: object.method()
Ticket.java▶ Try it
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);
    }
}
Output
Fare for 300 km: Rs. 600
Navika in berth 23
Srihitha in berth 24
Tickets issued: 2

How it works

  1. 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).
  2. describe (lines 10 to 12) has no static, so it's an instance method. It reads name and berth, which belong to a particular ticket.
  3. t1.describe() uses t1's fields; t2.describe() uses t2's. Same method, different objects, different results.
  4. issued is 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 methodInstance method
Called asTicket.fareFor(300)t1.describe()
Needs an object firstNoYes
Can use static fields and methodsYesYes
Can use instance fields and methods directlyNoYes
Has thisNoYes

A static method has no object, so it has no name or berth to read. Trying gives one of the most common beginner errors:

Compiler output
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 whenMake 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 stateIt 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

  1. Easy: Write a static method kmToMiles(double km) and call it from main without creating any object.
  2. Harder: Add an instance field int km to Ticket and an instance method fare() that calls the static fareFor(km). Print each ticket's fare.

Quick check: Static vs instance methods

4 questions
1How do you call a static method fareFor of class Ticket?
2Can a static method read an instance field directly?
3Why do t1.describe() and t2.describe() print different text?
4Which is a good candidate for a static method?

Key Takeaways

Was this tutorial helpful?

Lesson 84 of 482