1.
How is it possible that Bar hack can have a variable called test with a value of over and at the same time have a variable called test with a value of t?
. java . / . ,
Bar hack = new Bar() {
public static final String test="over";
test bar annonymous (Bar $1.class), Bar.class. test "t" - , Bar.class. , func, , , , "over"
public void func(){
System.out.println("Ideone.Meh.hack.func -- Test: "+test);
}
, Bar.test, test , Bar, .
public void func(){
System.out.println("Ideone.Meh.hack.func -- Test: "+Bar.test);
}
2.
Also, is Bar hack = new Bar(); inheriting from itself? If hack is an instance of Bar, how can it have a super that is of type Bar and contains a different test?
, , Bar.
Bar hack = new Bar(){
public static final String test="over";
public String getTest(){
System.out.println("Ideone.Meh.hack.super.test: " + Bar.super.getTest());
return test;
}
public void Meh(){
System.out.println("Ideone.Meh.hack.Meh()");
func();
Bar.Sup();
Bar.super.Meh();
}
public void func(){
System.out.println("Ideone.Meh.hack.func -- Test: "+test);
}
};
True hack - , - , , , .