0

Having error pls help on java

Error; a reference to an enclosing class is required. I want to print out the substraction class but when I create a new object of the class it gives me that error while an object of the Addition class works well class Addition { int add(){ int a= 2; int c= 3; System.out.println(a + c); return a + c; } class Substraction extends Addition { @Override int add(){ int a= 2; int b= 3; System.out.println(a-b); return a - b; } } public static void main(String args[]) { Substraction n= new Substraction(); n.add(); } }

13th Aug 2019, 5:46 AM
Dprincebh
Dprincebh - avatar
3 ответов
+ 2
main() is static and can call only static methods and class variables, but you can create non-static object and call add() from this new object: public static void main(String args[]) { new Addition().main2();} void main2(){ Substraction n= new Substraction(); ... }
13th Aug 2019, 7:50 AM
zemiak
+ 2
Cause you use non static variable in static method
13th Aug 2019, 7:52 AM
Marina Vasilyova
Marina Vasilyova - avatar
0
Thanks
13th Aug 2019, 9:45 AM
Dprincebh
Dprincebh - avatar