re 多个匿名内部类实例

发布于 2024-10-14 18:44:23 字数 330 浏览 6 评论 0原文

这与我对此线程中提供的问题的回答有关: 内部类是轻量级的吗?

我记得在阅读中,如果您只能从单个匿名内部类创建一个对象,并且出于这个原因,如果您想说创建一个 ActionListener 类并想要创建多个来自这个类的对象(不使用反射),不使用匿名内部类,而是使用私有内部类或独立类,但人们告诉我我错了。有人可以帮我澄清一下吗?请检查链接,因为它包含更多详细信息,但如果有任何不清楚的地方,请询问!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情独悲 2024-10-21 18:44:23

你可以创建任意数量的匿名类对象,你可以在代码中的一个地方创建它们(除非你复制代码)

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.");
      }
   });
}

这段代码将创建100万个同一个类的对象。

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.

桃气十足 2024-10-21 18:44:23

我不清楚。也许如果我们梳理规范,我们会发现内部类应该与普通类一样对待的证据。然而本质上,内部依赖于外部实例,该类不存在于实例之外。这与“正常”类不同,“正常”类的存在基本上是永久的。当然,两个外部实例的两个内部类在某种程度上彼此相关,由相同的源代码创建,但这并不意味着它们必须相同甚至相等。

有证据表明 Java 设计者的意图是这样的,即内部类实际上存在于外部实例的范围内。例如,奇怪的语法outerInstance.new InnerClass()。例如,没有静态变量,没有内部类的静态初始值设定项。在类卸载的讨论中[1],我们看到这个论点并不真正适用于内部类,可以想象内部类是可以被卸载的!可以想象,VM 会为每个新的外部实例创建一个新的内部类。

实际上并非如此,内部类确实被视为与普通类相同。但从概念上讲,我总是以不同的方式看待它们,将它们视为实例私有类。

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

更新:根据 [2]

如果两个引用类型具有相同的二进制名称,则它们是相同的运行时类型

并且 [3],

则它们是相同的运行时类型

匿名类的二进制名称 (§15.9.5) 由其直接封闭类型的二进制名称组成,后跟 $,后跟非空数字序列。

因此,一个匿名类有一个二进制名称,因此只有一种运行时类型。该规范保证匿名类的不同实例具有相同的类。

[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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文