内部类轻量级? [英] Are Inner Classes lightweight?

查看:126
本文介绍了内部类轻量级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内部类比普通类更轻量,或者在最后java编译内部类就像正常类?



我知道java中的类不是非常轻量级自己,他们占用了部分permgen内存,所以我想知道如果最好使用闭包类函数作为内部类,或者如果标准类也会很好吗?

.class 文件。例如:

  class Outer {
class Inner {

}

对象函数(){
return new Object(){

};
}
}

将生成三个 .class 文件, Outer.class 外部$ Inner.class Outer $ 1.class 。他们不是比其他类更轻量级,并且(据我所知),从性能角度来看,使用一个没有优势。当然,内部类,特别是匿名内部类在上下文中非常有用,在这种情况下,常规类更难于编码,但这是一个单独的问题。


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?

解决方案

Inner classes and anonymous inner classes both compile down to .class files. For example:

class Outer {
     class Inner {

     }

     Object function() {
          return new Object() {

          };
     }
}

Will generate three .class files, Outer.class, Outer$Inner.class, and Outer$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.

这篇关于内部类轻量级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆