I am new to java and trying some accessing methods and i encountered something that i do not understand. The code below working fine, prints 9 and not giving any compilation errors. I think this code should give a compilation error and number should be inaccessible from the test method, since new Human() is an instance of a totally different class. Can anybody explain me what is happening here ?
public class Test{
public static void main(String[] args) {
int number = 9;
test("holla",new Human(){
@Override
void test() {
// TODO Auto-generated method stub
System.out.println(number); // I think this line should not compile
}
});
}
private static void test(String a ,Human h){
h.test();
}
}
Human Class
public abstract class Human {
abstract void test();
}