Skip to the content.

Calling Class Methods

Student Teaching Lesson

Popcorn Hack

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) {
        // OK: static method called from static context (recommended style)
        Bro.sayCatchphrase();

        // ERROR originally: sayHi(); // <-- cannot call instance method from static context

        // Create an instance and call the instance method
        Bro alex = new Bro("Alex");
        alex.sayHi();

        // OK but prefer class call for statics
        Bro.sayCatchphrase();
    }
}


Homework Hack

class Fighter {
    // Instance variables
    String name;
    int power;
    int health;
    String weapon; // your own variable!

    // Static variable
    static double fightDuration = 2.5; // in minutes

    // Constructor
    public Fighter(String name, int power, int health, String weapon) {
        this.name = name;
        this.power = power;
        this.health = health;
        this.weapon = weapon;
    }

    // Instance methods
    void attack(Fighter opponent) {
        System.out.println(name + " attacks " + opponent.name + " with " + weapon + "!");
        opponent.health -= this.power;
        if (opponent.health < 0) opponent.health = 0;
    }

    void printStatus() {
        System.out.println(name + " → Health: " + health + ", Power: " + power + ", Weapon: " + weapon);
    }

    void heal(int amount) {
        health += amount;
        System.out.println(name + " heals for " + amount + " points. Total health: " + health);
    }

    // Class (static) methods
    static Fighter strongerFighter(Fighter f1, Fighter f2) {
        if (f1.power > f2.power) return f1;
        else if (f2.power > f1.power) return f2;
        else return null;
    }

    static void beginBattle(Fighter f1, Fighter f2) {
        System.out.println("\n Battle begins between " + f1.name + " and " + f2.name + "!");
        System.out.println("Estimated duration: " + fightDuration + " minutes\n");
    }

    static void battleFact() {
        System.out.println("Fun Fact: The average medieval duel lasted under 3 minutes!");
    }

    // Main method
    public static void main(String[] args) {
        Fighter hero = new Fighter("Aiden", 30, 100, "Sword");
        Fighter villain = new Fighter("Rex", 25, 120, "Axe");

        beginBattle(hero, villain);
        battleFact();

        hero.attack(villain);
        villain.attack(hero);
        hero.heal(10);

        hero.printStatus();
        villain.printStatus();

        Fighter stronger = strongerFighter(hero, villain);
        if (stronger != null)
            System.out.println("Stronger fighter!: " + stronger.name);
        else
            System.out.println("Both fighters have equal strength.");
    }
}
Fighter.main(null);

 Battle begins between Aiden and Rex!
Estimated duration: 2.5 minutes

Fun Fact: The average medieval duel lasted under 3 minutes!
Aiden attacks Rex with Sword!
Rex attacks Aiden with Axe!
Aiden heals for 10 points. Total health: 85
Aiden → Health: 85, Power: 30, Weapon: Sword
Rex → Health: 90, Power: 25, Weapon: Axe
Stronger fighter!: Aiden