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(); } }
3 Antworten
+ 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();
...
}
+ 2
Cause you use non static variable in static method
0
Thanks