Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Classes, Objects and Memory

    Instance vs Static

    An instance member (field or method) belongs to each object. It needs an object to be used: priya.addBonus(20).

    Chapter 3 of 104 min read

    The Plain Picture#

    In a school, each student has their own roll number and marks. But the school itself has one name, one principal, and one count of "total students enrolled". Asking a student "what are your marks?" makes sense. Asking the school "what are your marks?" doesn't.

    The Real Names#

    • An instance member (field or method) belongs to each object. It needs an object to be used: priya.addBonus(20).

    • A static member belongs to the class itself — one shared copy, no object needed: Student.created, Math.max(...).

    Under the Hood#

    Java
    class Student {
        String name;
        int score;
        static int created = 0; // ONE shared counter for the whole class
    
        Student(String name, int score) {
            this.name = name;
            this.score = score;
            created++; // every new Student bumps the shared counter
        }
    
        void addBonus(int points) {
            // instance method: works on THIS student's score
            score += points; // (same as this.score += points)
        }
    
        boolean isPassing() {
            return score >= 150;
        }
    }
    Java
    Student priya = new Student("Priya", 180);
    
    Student rahul = new Student("Rahul", 0);
    
    priya.addBonus(20);
    
    System.out.println(priya.name + " " + priya.score + " " + priya.isPassing()); // Priya 200 true
    
    System.out.println(rahul.name + " " + rahul.score + " " + rahul.isPassing()); // Rahul 0 false
    
    System.out.println("created: " + Student.created); // created: 2
                     Student class (loaded once)
                    ┌──────────────────────────┐
                    │ static created = 2       │  ← one copy, shared
                    │ methods: addBonus, ...   │
                    └──────────────────────────┘
            ┌──────────────────┐        ┌──────────────────┐
            │ name  = "Priya"  │        │ name  = "Rahul"  │   ← each object: own fields
            │ score = 200      │        │ score = 0        │
            └──────────────────┘        └──────────────────┘

    The key rule: a static method has no this — it isn't running "on" any object — so it can't use instance fields or instance methods directly:

    Java
    static void broken() {
        score = 10; // compile error: non-static variable score cannot be referenced from a static context
    }

    That's why main (static) had to call only static methods in Java Basics. Now you know what to do instead: create an object and call its instance methods.

    When to make something static:

    • utility methods that don't need any object's data — Math.max, Integer.parseInt, a static int score(int correct) helper;

    • constants — static final int PASS_MARK = 150;;

    • shared counters or caches (carefully — shared state + threads = race conditions, Operating Systems Ch 8 and Concurrency).

    Everything that describes one particular student should be instance, not static.

    Check yourself

    3 questions
    1

    Instance vs static field?

    Show answer

    Each object has its own instance field; a static field has one copy shared by the whole class.

    2

    Why can't a static method read an instance field directly?

    Show answer

    A static method isn't called on any object, so there's no this whose field it could read.

    3

    Should grade(int points) be static or instance? Should addBonus? Why?

    Show answer

    grade(int points) can be static — it only uses its parameter. addBonus must be instance — it changes a particular student's score.