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!

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.

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 class Main {
    public static void main(String[] args) {
        Bro.sayHi(); // ??
        Bro.sayCatchphrase(); // ??
        
        Bro alex = new Bro("Alex");
        alex.sayHi(); // ??
        alex.sayCatchphrase(); // ??
    }
}

Chart for the Bro Example

  Static Variable Non-Static Variable
In static (Class) method ✅ Can use ❌ Cannot use
In instance method ✅ Can use ✅ Can use

Homework Hacks - Calling class methods

Part 1 — Fix the Bro Code

Instructions

The Bro class has both static and instance methods.
Your job: fix the code so it runs without errors and prints correctly.

Steps:

  1. Read the code below.
  2. Identify which lines cause errors and why.
  3. Fix the broken lines so it prints:
    My name is Kush
    Sup bro?
    

Hint:

  • You can call a static method using the class name.
  • You can call a non-static (instance) method only with an object.
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);
    }

    public static void sayClassicBroSaying() {
        System.out.println(classicBroSaying);
    }
}

public class Main {
    public static void main(String[] args) {
        // TODO: Fix these method calls
        Bro.sayMyName(); // won't work
        Bro.sayClassicBroSaying(); // works

        Bro kush = new Bro("Kush");
        // TODO: Add the correct calls here
    }
}

Homework Hack: Bro Methods & Static Power (Part 2)

Instructions

In the lesson, you learned that static methods and variables belong to the class, not any single object.
Let’s put that to the test by building a Bro Club that keeps track of how many Bros exist — using static features to share information across all Bros.

Your goal:
Use a static variable and static methods to manage the club’s membership.

Steps:

  1. Add a static variable in BroClub to count how many Bros have joined.
  2. Add a static method to register a Bro (increase the count).
  3. Add another static method to display how many total Bros are in the club.
  4. Modify the Bro constructor so each new Bro automatically registers in the club.
  5. In main(), create multiple Bros and print the total.

Hint:
Remember — static variables and methods are called with the class name, not the object.
Example:
BroClub.showTotalBros();

class Bro {
    String name;

    public Bro(String name) {
        this.name = name;
        // TODO: Register this Bro in the club using a static method
    }

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

class BroClub {
    // TODO: Add a static variable to count total Bros
    // TODO: Add a static method to register a Bro
    // TODO: Add a static method to show total Bros
}

public class Main {
    public static void main(String[] args) {
        // TODO: Create at least two Bros
        // TODO: Use the static method to display the total Bros
    }
}

REMEMBER

Why It Connects to the Lesson

This exercise SHOULD help you see the difference between calling methods from the class (static) and calling methods from an object (instance).
You’ll understand why one of these lines causes an error — and how to fix it by using the correct call style.