内部类是轻量级的吗?
是内部类比普通类更轻量级,还是说最终java编译内部类就像普通类一样?
我知道java中的类本身并不都是非常轻量级的,并且它们占用了permgen内存的一部分,所以我想知道是否最好使用类似闭包的函数作为内部类,或者标准类是否也可以?
Are inner classes more lightweight than normal classes, or in the end java compiles inner classes just like normal classes?
I know classes in java are not all very lightweight themselves, and they occupy part of the permgen memory, so I'd like to know if it's best to use closure-like functions as inner classes, or if standard classes would also do fine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
内部类和匿名内部类都编译为
.class
文件。例如:将生成三个
.class
文件,Outer.class
、Outer$Inner.class
和Outer$1.class< /代码>。它们并不比其他类更“轻量级”,并且(据我所知)从性能角度来看,使用其中一个类并没有比其他类有任何优势。当然,内部类,尤其是匿名内部类,在常规类难以编码的情况下非常有用,但这是一个单独的问题。
Inner classes and anonymous inner classes both compile down to
.class
files. For example:Will generate three
.class
files,Outer.class
,Outer$Inner.class
, andOuter$1.class
. They aren't more "lightweight" than other classes, and (to the best of my knowledge) there's no advantage to using one over the other from a performance perspective. Of course, inner classes and especially anonymous inner classes are really useful in contexts where regular classes are harder to code, but that's a separate issue.内部类仍然是类,它们仍然必须由类加载器加载。如果有的话,事实恰恰相反。非静态内部类可以防止父类被垃圾收集,因为它具有对拥有它的类的引用。
Inner classes are still classes and they still have to be loaded by a ClassLoader. If anything, the opposite is true. A non-static inner class can keep the parent class from being garbage collected since it has a reference to the class which owns it.
它们并不轻量,但也有局限性。 AFAIK,您不能创建多个匿名内部类的实例,因此如果您需要这样做,则必须使用非匿名类。
编辑1:感谢所有的评论和澄清。请帮助我更好地理解这一点...我知道您可以拥有匿名内部类的多个实例,但是如果我声明一个匿名内部 ActionListener 对象,那么我如何拥有该类的多个实例 并且只有那个类不使用反射?提前致谢(或者我应该在我自己的问题中问这个?)!
好吧,因为我没有被咬……让我用代码来演示。假设我在这里创建一个匿名的 ActionListener:
我创建了一个匿名的 ActionListener,并且还一下子创建了这个类的一个对象。我一直被教导的,我在帖子顶部提到的(并因此受到批评)是,即使不是不可能(没有反射的魔力)创建这个匿名类的另一个对象也是很困难的,只能制作一个物体,并且在只需要一个物体的情况下,这没问题。但在其他情况下,这就不好了。当然,您可以创建多个类似的匿名 ActionListener 类,就像在 for 循环中一样:
但即便如此,此处创建的每个匿名类都是不同的。如果多个 JButtens 使用相同类型的侦听器,那么这可能很重要,它具有状态,并且其行为取决于此状态。同意?不同意?预先感谢您的任何意见!
They're not light-weight, but they have limits. AFAIK, you can't create more than one instance of an anonymous inner class, and so if your needs require this, you must use a non-anonymous class.
edit 1: Thanks for all the comments and clarifications. Please help me understand this better... I understand that you can have multiple instances of anonymous inner classes, but if I declare a single anonymous inner ActionListener object for instance, how can I have multiple instances of that class and only that class without using reflection? Thanks in advance (or should I ask this in my own question?)!
OK, since I'm not getting any bites... Let me demonstrate with code. Say I create an anonymous ActionListener here:
I create an anonymous ActionListener and also create an object of this class in one fell swoop. What I've always been taught, what I mentioned at the top of my post here (and got dinged for), was that it is difficult if not impossible (without the magic of reflection) to make another object of this anonymous class, that only one object can be made, and in situations where only one object is needed, this is fine. But in other situations, it's not fine. And sure, you could create multiple similar anonymous ActionListener classes, as in a for loop:
But even so, each anonymous class created here is different. This may have significance if this same type of the listener is used by multiple JButtens, that it has state, and its behavior depends on this state. Agree? Disagree? Thanks in advance for any input!