public class Test {
class Foo {
int frob() {
return 7;
}
}
class Bar extends Foo {
@Override
int frob() {
return 8;
}
}
class Baz extends Foo {
@Override
int frob() {
return 9;
}
}
public static int quux(Bar b) {
return b.frob();
}
public static void main(String[] args) {
System.out.println(quux(new Bar()));//this line gives non-static variable this cannot be referenced from a static context
}
}
public class Test { class Foo { int frob() { return 7; } }
Share
You can fix this by declaring the nested classes as static, OR by instantiating Bar in the context of an instance of Test.
It fails because (the non-static) Bar must be instantiated in the context of an existing Test class instance; since main is static, there is no such beast.