What is a “Class Method?”

A class method (or static method in Java) belongs to the class itself, not to any specific object.

(You can call these methods directly using the class name, without needing to create an object first.)

Key characteristics of static methods:

  • They are declared with the static keyword.
  • They can be called using the syntax: ClassName.methodName().
  • They cannot use instance variables or instance methods directly (because they don’t carry the state of the object).
  • They are useful for utility functions or operations that are not tied to the state of a particular object.

Printer Example

class Printer {
    // This IS a CLASS METHOD. It can be called WITHOUT creating an instance.
    public static void print(String message) {
        System.out.println(message);
    }

    // This is NOT a CLASS METHOD. You need an instance to call it.
    public void explode() {
        System.out.println("Boom");
    }
}

Printer.print("Hello, World!"); // This will work!
Printer.explode(); // This will NOT work!
Hello, World!



|   Printer.explode(); // This will NOT work!

non-static method explode() cannot be referenced from a static context

Dude example

class Dude {
    String name;

    public Dude(String name) {
        this.name = name;
    }

    public static void SayName() {
        System.out.println("My name is " + name); // This will NOT work! cause name is an instance variable.
    }
}

// This will not compile since sayAnotherName is a STATIC (CLASS) METHOD and cannot access the INSTANCE variable 'name'.

Bro example

class Bro {
    String name;
    static String classicBroSaying = "Sup bro?";

    public Bro(String name) {
        this.name = name;
    }

    public void sayMyName() {
        System.out.println("My name is " + name); // This will work!
    }

    public static void sayClassicBroSaying() {
        System.out.println(classicBroSaying); // This will work too!
    }
}

Bro kush = new Bro("kush");
kush.sayMyName(); // This will work!

Bro.sayClassicBroSaying(); // This will work! because the classicBroSaying variable is STATIC.
Bro.sayMyName(); // This will NOT work! because sayMyName is NOT a STATIC method.
My name is kush
Sup bro?



|   Bro.sayMyName(); // This will NOT work! because sayMyName is NOT a STATIC method.

non-static method sayMyName() cannot be referenced from a static context

Popcorn Hack: Which is an instance method or static method

What to do

You’ve learned that static methods belong to the class and instance methods belong to the object.

Look at the code below and decide which lines will work — and which will cause an error.
Then fix the wrong ones.

class Bro {
    String name;
    static String catchphrase = "Sup bro?";

    public Bro(String name) {
        this.name = name;
    }

    public void sayHi() {
        System.out.println("Hey, I'm " + name);
    }

    public static void staticSayHi() {
        System.out.println("Hey, I'm Bro");
    }

    public static void sayCatchphrase() {
        System.out.println(catchphrase);
    }
}

public class Main {
    public static void main(String[] args) {
        // Bro.sayHi(); Doesn't work - static calling on instance
        Bro.staticSayHi(); // Fixed by adding a static hi method for static Bro
        Bro.sayCatchphrase(); // Works - all static
        
        Bro alex = new Bro("Alex");
        alex.sayHi(); // Works - instance calling on instance
        alex.sayCatchphrase(); // Works - instance calling on static
    }
}

Main.main(new String[]{});
Hey, I'm Bro
Sup bro?
Hey, I'm Alex
Sup bro?

Chart for the Bro Example

  Static Variable Non-Static Variable
In static (Class) method
In instance method