Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java anonymous class and reachable private variable

interface Test {
public void test();
}

public class TestMain {
private String h = "AAA";

public static void main(String[] args) {
    TestMain t = new TestMain();
}

public TestMain() {
    Test t = new Test() {
        public void test()  {
            System.out.println( h );
        }
    };

    t.test();
}

}

The above source works well.

But I think the variable 'h' should be unreachable from the anonymous class. I need to know why it works well.

Thank you for all of your help in advance!

like image 731
Jane Garcia Avatar asked Oct 31 '11 04:10

Jane Garcia


People also ask

Can you access a private variable outside of its class?

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Can Anonymous classes be private?

An anonymous class cannot define any static fields, methods, or classes, except for staticfinal constants. Interfaces cannot be defined anonymously, since there is no way to implement an interface without a name. Also, like local classes, anonymous classes cannot be public, private, protected, or static.

Can anonymous class be passed as a parameter?

Technically, no, because anonymous classes can't have constructors. However, classes can reference variables from containing scopes. For an anonymous class these can be instance variables from the containing class(es) or local variables that are marked final.

Can an anonymous inner class be declared private?

An anonymous inner class also has the same restriction as to the local inner class with respect to its members. Anonymous inner class cannot be declared as public, private, protected, or static. It cannot access local variables of its enclosing scope that are not declared as final or effectively final.


2 Answers

Each instance of a non-static inner class has an enclosing instance -- an instance of the outer class which it tied to it via a reference variable stored in the inner class object. All the members of the enclosing instance of available to the inner class object via that reference.

The compiler takes care of adding that reference variable, of course, as well as some special accessor functions that the inner class object can use to get to the outer class object's private members. You can see these accessor functions by disassembling the outer class using javap -c.

like image 84
Ernest Friedman-Hill Avatar answered Sep 23 '22 09:09

Ernest Friedman-Hill


The basic rule for determining accessibility to private members is, according to section 6.6.1 of the Java Language Specification:

[I]f the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class that encloses the declaration of the member or constructor.

like image 41
Ted Hopp Avatar answered Sep 24 '22 09:09

Ted Hopp