Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an outer anonymous class's field from an inner anonymous class

To access the field x of an outer class A from an inner class B, I realize that you can use "A.this.x". But what if the outer class is also anonymous? For example,

public class Main1 {
    public static void main(String[] args) {
        Comparable c1 = new Comparable(){
            int x = 3;
            public int compareTo(Object o) {
                Comparable c2 = new Comparable(){
                    int x = 4;
                    public int compareTo(Object o) {
                        return x;  // <-- THIS LINE
                    }
                };
                return c2.compareTo(o);
            }
        };
        System.out.println(c1.compareTo(null));
    }
}

When this code is run, the value of 4 is printed because that is the value of c2's field x. However, I would like to change the line marked "THIS LINE" so that it returns the outer class's x (that is, c1's field x, with the value 3). If the outer class (that is, c1's class) were a named class A, then I could replace

return x;

with

return A.this.x;

But since the outer class is also anonymous, I don't have a name to use.

Question: Is there a way to modify the line labeled "THIS LINE" so that it refers to c1's field x rather than c2's, without changing the anonymous classes into named classes?

I realize this code is really ugly and it is not good programming style to use anonymous classes this way, but the code is being generated by another program, and this is the easiest way to implement the generator.

like image 476
Zoot101 Avatar asked Feb 11 '09 14:02

Zoot101


People also ask

How do you refer to outer class from inner class?

To reference the inner class instance itself, from within the inner class code, use "this". To reference the outer class instance from within the inner class code, use "Town. this".

Can inner classes access outer class private members?

Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

Can inner class calling Outer method?

No, you cannot assume that the outer class holds an instance of the inner class; but that's irrelevant to the question. You're first question (whether the inner class holds an instance of the outer class) was the relevant question; but the answer is yes, always.

How do you use an anonymous inner class?

An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class. Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.


2 Answers

I would avoid hiding the other variable by choosing a name other than x.

like image 146
Outlaw Programmer Avatar answered Oct 09 '22 11:10

Outlaw Programmer


The simple answer is to not shadow the variables:

public static void main(String[] args) {
    Comparable c1 = new Comparable() {
        int x = 3;
        public int compareTo(Object o) {
            Comparable c2 = new Comparable() {
                //changed this name
                int y = 4;
                public int compareTo(Object o) {
                    return x;
                }
            };
            return c2.compareTo(o);
        }
    };
    System.out.println(c1.compareTo(null));
}

Output:

3

Given that you are working with generated code, is this an option for you?

like image 2
matt b Avatar answered Oct 09 '22 11:10

matt b