Is there a performance overhead to a private inner class in Java?

情到浓时终转凉″ 提交于 2019-12-18 13:09:26

问题


When I have inner classes with private methods or fields the compiler has to create synthetic package-protected accessor methods to allow the outer class to access those private elements (and vice-versa).

To avoid that, I usually make all fields and methods and constructors package-protected instead of private.

But how about the visibility of the class itself? Is there an overhead to

 private static class A {
      A(){}
 }

versus

 static class A {
      A(){}
 }

Note that the constructor is package-protected in both cases, or does making the class private change that?


回答1:


Have you tried compiling it and comparing the byte code? Here are my results. For:

public class Example {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
  private static class A {
    A(){}
  }
}

The above yields the following *.class files:

-rw-r--r--    1 michaelsafyan  staff   238 Feb 10 00:11 Example$A.class
-rw-r--r--    1 michaelsafyan  staff   474 Feb 10 00:11 Example.class

Now, if I move the class files, delete the private modifier, and recompile, I get:

 -rw-r--r--    1 michaelsafyan  staff   238 Feb 10 00:15 Example$A.class
 -rw-r--r--    1 michaelsafyan  staff   474 Feb 10 00:15 Example.class

If you look at the VM Spec on class files, you'll see that there is a constant-sized bit field for specifying the access modifiers, so it should not be any surprise that the generated files are the same size.

In short, your access modifiers won't affect the size of the generated byte code (it also should not have any performance impact, either). You should use the access modifier that makes the most sense.

I should also add that there is a slight difference if you change the inner class from being declared static to not being declared static, as it implies an additional field referencing the outer class. This will take up slightly more memory than if you declared the inner class static, but you'd be insane to optimize for this (use static where it makes sense, and where you need it to be non-static, make it non-static, but don't convolute your design just to save a pointer of memory here or there).




回答2:


There should be no performance difference between a private inner class and a non-private inner class.

There should be no performance difference between a static inner class (private or not) and an outer class.

There is a small performance difference between a static inner class and a non-static inner class. This difference is due to the fact that the non-static case has an hidden reference to the instance of its enclosing class. This is passed as an extra parameter to the inner classes constructor, and stored in a hidden variable.




回答3:


It is very unlikely this will ever cause any significant slow-down.

One issue to be aware of with non-static inner classes is that they contain a reference to the instance of the enclosing class.

As such, this can cause 'memory leaks', as the enclosing instance can't be garbage collected. If you pass instances of inner classes out to callers, you are also passing out an instance of the enclosing class.

This is not the case with static inner classes!

Making all fields package protected to prevent synthetic methods is not advisable. You are giving up encapsulation which is a valuable thing to have. What are your concerns? Size of your class files for the additional code?



来源:https://stackoverflow.com/questions/4953597/is-there-a-performance-overhead-to-a-private-inner-class-in-java

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