Access to internal anonymous class members

Is there any way besides using reflection to access members of an anonymous inner class?

+6
java anonymous-class
source share
7 answers

Anonymous inner classes have a type, but not a name.

You can access fields not specified by the specified supertype. However, after assigning a variable of type named, the interface is lost.

Obviously, you can access the fields inside the inner class. One way to add code is through the instance initializer:

final AtomicInteger y = new AtomicInteger(); new Runnable() { int x; { x = 5; doRun(this); y.set(x); } public void run() { ... blah ... } }; 

The value returned by the anonymous internal expression of the class is of an anonymous type, so you have one chance to use it outside the class itself:

 final int y = new Runnable() { int x; { x = 5; doRun(this); } public void run() { ... blah ... } }.x; 

You can also pass it using a method declared similarly:

 <T extends Runnable> T doRun(T runnable); 
+9
source share

Instead of an anonymous class, you can use local classes. Take a look:

 public class Test { public static void main(String... args) { class MyInner { private int value = 10; } MyInner inner = new MyInner(); System.out.println(inner.value); } } 

However, you can only reference the MyInner type only in the method tag. Thus, outside the method you cannot use your fields / methods that are not declared in its superclass ( java.lang.Object in this case) or the interface.

+9
source share
 public class AccessAnonymous { private Runnable runnable; // to have instance of the class public static void main(String[] args) throws Exception { AccessAnonymous a = new AccessAnonymous(); aa(); // init field Class clazz = a.runnable.getClass(); Field field = clazz.getDeclaredField("i"); field.setAccessible(true); int int1 = field.getInt(a.runnable); System.out.println("int1=" + int1); } public void a() { runnable = new Runnable() { private int i = 1; public void run() { i = 90; } }; runnable.run();// change value } } 
+1
source share

In the case of anonymous classes, there is also a trade-off between the clutter caused by the class and the convenience of its anonymity. Complex classes rarely belong as anonymous, but rather are called private inner.

In most anonymous classes, we only need to “feed” knowledge and do it during construction. In several anonymous classes (e.g. return vehicles), we also take care of a single return value.

As we know, data elements should not be accessed directly, but rather, getter installers. That is, if you find yourself in a situation where you have added many getters and setters, you are probably still wrong and should not use an anonymous class.

+1
source share

If it implements an interface or extends an existing class, you can access the elements defined in the interface or base class.

0
source share

Mr. Fooz is right, except that only constant members can define interfaces. the best way would be to add getter / setter methods to your interface and then use them to get your value. but then for each anonymous class you will need to define these methods (kind of pain).

0
source share

If you want to read convenient, supported code, do not use anonymous classes. If you use anonymous classes and want to read convenient, supported code, do not use anonymous classes when you need to access an element of this inner class. There are ways to do this, but I ask you not to use any of these hacks. Readability surpasses all other virtues.

0
source share

All Articles