re More than one instance of an anonymous inner class

﹥>﹥吖頭↗ 提交于 2019-12-01 14:07:15

问题


This is in relation to my answer to a question provided in this thread: Are Inner Classes lightweight?

I remember from my reading that if you can only create one object from a single anonymous inner class, and for this reason, if you want to say create an ActionListener class and want to create multiple objects from this one class (not using reflection), to not use an anonymous inner class but rather either a private inner class or a stand along class, but folks are telling me I'm wrong. Can someone please clarify this for me? Please check the link as it contains more details, but if anything is unclear, please ask away!


回答1:


You can create any number of anonymous class objects, you can create them in one place in your code (unless you copy the code)

ExecutorService service = ...
for(int i=0;i<1000*1000;i++) {
   final int finalI = i;
   service.submit(new Runnable() {
      public void run() {
         System.out.println("Task "+finalI+" run.");
      }
   });
}

This code will create 1 million objects of the same class.




回答2:


It is unclear to me. Maybe if we comb through specs we'll find evidence that inner classes should be treated the same as normal classes. However in spirit, an inner class depends on the outer instance, the class doesn't exist beyond the instance. This is different from "normal" classes whose existence is basically perpetual. Two inner classes of two outer instances of course are somewhat related each other, being created by the same source code, yet that doesn't mean they must be identical or even equal.

There are evidence that Java designers intended this way, that an inner class in spirit lives within the scope of the outer instance. For example, the curious syntax outerInstance.new InnerClass(). For example, no static variables, no static initializers for inner classes. In the discussion of class unloading [1], we see that the argument doesn't really apply to inner classes, it's conceivable that inner classes can be unloaded! It is conceivable that a VM creates a new inner class for each new outer instance.

Practically that's not the case, inner classes are indeed treated the same as normal classes. But conceptually, I'll always think of them differently, as instance-private classes.

[1] http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7

Update: according to [2]

Two reference types are the same run-time type if They ... have the same binary name

and [3]

The binary name of an anonymous class (§15.9.5) consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits.

So one anonymous class has one binary name, therefore only one run-time type. The spec guarantees us that different instances of an anonymous class have identitcal class.

[2] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.4

[3] http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909



来源:https://stackoverflow.com/questions/4820031/re-more-than-one-instance-of-an-anonymous-inner-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!