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

Printer Example

This example demonstrates that a static (class) method can be called without creating an object, while an instance method requires one.
Printer.print() works because it’s static, but Printer.explode() causes an error since it’s an instance method and needs an object of Printer to be called.

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'.

Dude Example

This shows that static methods cannot access instance variables.
The method SayName() tries to use name, which belongs to an instance, not the class itself. To fix this, SayName() must be non-static or receive name as a parameter.

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.

Bro Example

This example contrasts how static and instance members behave together.
classicBroSaying is static, meaning it belongs to the class and can be used in a static method.
name is an instance variable, so only instance methods like sayMyName() can access it.
You call static methods with the class name, but instance methods with an object.

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 sayCatchphrase() {
        System.out.println(catchphrase);
    }

    public static void main(String[] args) {
        // Will this work?
        sayCatchphrase();

        // What about this one?
        sayHi();

        Bro alex = new Bro("Alex");
        alex.sayHi();
        alex.sayCatchphrase();
    }
}
|           sayHi();

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

Chart for the Bro Example

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

Real-Life Example: Bank Account

class BankAccount {
    String owner;
    double balance;
    static double interestRate = 0.05; // shared by all accounts

    public BankAccount(String owner, double balance) {
        this.owner = owner;
        this.balance = balance;
    }

    // Instance method → affects a single account
    public void deposit(double amount) {
        balance += amount;
        System.out.println(owner + " deposited $" + amount + ". New balance: $" + balance);
    }

    // Instance method → shows this account’s balance
    public void showBalance() {
        System.out.println(owner + "'s current balance: $" + balance);
    }

    // Static method → shared across all accounts
    public static void setInterestRate(double newRate) {
        interestRate = newRate;
        System.out.println("New interest rate set to " + (interestRate * 100) + "% for all accounts.");
    }

    public static void main(String[] args) {
        BankAccount alex = new BankAccount("Alex", 500);
        BankAccount brooke = new BankAccount("Brooke", 1000);

        alex.deposit(200);
        brooke.deposit(300);

        alex.showBalance();
        brooke.showBalance();

        BankAccount.setInterestRate(0.07); // static call through the class
    }
}

Bank Account Example

This example shows how static members represent shared data or behavior across all objects.
Each BankAccount object has its own balance, but all accounts share the same interestRate.

The deposit() and showBalance() methods are instance methods because they work on a single account’s data.
The setInterestRate() method is static because it changes a value shared by every account.