Uttaran
Uttaran

Reputation: 59

Is there a way to access an anonymous class inside another anonymous class?

interface A {
    void show();
}

public class Static {
    public static void main(String args[]) {
        A a = new A(){
            public void show(){
                System.out.println("In anonymous Class");
                A b =new A(){
                    public void show(){
                        System.out.println("In nested Anonymous Class");
                    }
                };
            }
        };
        //a.show();
    }
}

If I want the to print "In nested Anonymous Class", what should I use instead of a.show()?

//EDITED LATER

Thanks guys But unfortunately mis-typed the code....I didn't mean anonymous class inside a method...but inside the class itself. Sorry for the mistake. Here is the corrected code

interface A {
    void show();
}

public class Static {
    public static void main(String args[]) {
        A a = new A() {
            public void show() {
                System.out.println("In anonymous Class");
            };

            A b = new A() {
                public void show() {
                    System.out.println("In nested Anonymous Class");
                }
            };
        };
        a.show();
    }
}

Upvotes: 2

Views: 67

Answers (3)

wvdz
wvdz

Reputation: 16651

Normally, it's not possible, since A is an interface and interfaces don't have fields. However, it is possible to access this field using reflection. It is a bit of hack though and I wouldn't suggest using this in the "real world"!

interface A {
    void show();
}

public class Static {
    public static void main(String args[]) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
        A a = new A() {
            public void show() {
                System.out.println("In anonymous Class");
            };

            public A b = new A() {
                public void show() {
                    System.out.println("In nested Anonymous Class");
                }
            };

        };
        // Get the anonymous Class object
        Class<? extends A> anonymousClass = a.getClass();
        // Get field "b"
        Field fieldB = anonymousClass.getField("b");
        // Get the value of b in instance a and cast it to A
        A b = (A) fieldB.get(a);
        // Show!
        b.show();
    }
}

Note: a better way might be to simply declare a getter on your interface for variable b.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200306

There is nothing you should use instead of a.show(). That line should be where you put it, and uncommented. Additionally you need b.show() inside:

public static void main(String args[]) {
  A a = new A(){
    public void show(){
      System.out.println("In anonymous Class");
      A b =new A(){
        public void show(){
          System.out.println("In nested Anonymous Class");
        }
      };
      b.show();
    }
  };
  a.show();
}

Upvotes: 0

dReAmEr
dReAmEr

Reputation: 7196

make a call to b.show(); just after class declaration.

                A b =new A(){
                    public void show(){
                        System.out.println("In nested Anonymous Class");
                    }
                };
                b.show();

Upvotes: 0

Related Questions