Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 663
Thilo Avatar asked Feb 10 '11 05:02

Thilo


2 Answers

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.

like image 154
Stephen C Avatar answered Nov 15 '22 18:11

Stephen C


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).

like image 34
Michael Aaron Safyan Avatar answered Nov 15 '22 18:11

Michael Aaron Safyan