class Demo {
void show() {
System.out.println(\"i am in show method of super class\");
}
}
class Flavor1Demo {
// An anonymous class with Demo as base c
You get an error by removing static
keyword from static Demo d = new Demo()
because you are using that object d
of class Demo
in main
method which is static
. When you remove static
keyword from static Demo d = new Demo()
, you are making object d
of your Demo
class non-static
and non-static
object cannot be referenced from a static
context.
If you remove d.show();
from main
method and also remove static
keyword from static Demo d = new Demo()
, you won't get the error.
Now if you want to call the show
method of Demo
class, you would have to create an object of your Demo
class inside main
method.
public static void main(String[] args){
Demo d = new Demo();
d.show();
}