-1

Why the method "div" doesn't work without static.? when just remove the static keyword it gives me the Error i.e .."\Playground:13: error: non-static method div(int, int) cannot be referenced from a static context System.out.println(div(42, 0));"

public class Program {

    static int div(int a, int b) throws ArithmeticException {
        if(b == 0) {
            throw new ArithmeticException("Division by Zero");
        } else {
            return a / b;
        }
    }

    public static void main(String[] args) {
        System.out.println(div(42, 2));
    }

}

1 Answers1

0

When you remove static, that makes div an instance method. So you must then change

System.out.println(div(42, 2));

to invoke div through an instance. For example,

System.out.println(new Program().div(42, 2));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249