Instance Methods

  • In Java, instance methods can only be called on an instance of an object
  • static methods do not need an instance
class MyCalculator {
    public static int add(int a, int b) { return a + b; }
    public int subtract(int a, int b) { return a - b; }
}

class App {
    public static void main(String[] args) {
        MyCalculator.add(1,2); // All good
        MyCalculator.subtract(1,2); // no good
        var calc = new MyCalculator();
        calc.subtract(1,2); // Good again
    }
}

18 / 31