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


when have inner classes private methods or fields compiler has create synthetic package-protected accessor methods allow outer class access private elements (and vice-versa).

to avoid that, make fields , methods , constructors package-protected instead of private.

but how visibility of class itself? there overhead to

 private static class {       a(){}  } 

versus

 static class {       a(){}  } 

note constructor package-protected in both cases, or making class private change that?

have tried compiling , comparing byte code? here results. for:

public class example {   public static void main(string[] args) {     system.out.println("hello world!");   }   private static class {     a(){}   } } 

the above yields 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 move class files, delete private modifier, , recompile, 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 @ vm spec on class files, you'll see there constant-sized bit field specifying access modifiers, should not surprise generated files same size.

in short, access modifiers won't affect size of generated byte code (it should not have performance impact, either). should use access modifier makes sense.

i should add there slight difference if change inner class being declared static not being declared static, implies additional field referencing outer class. take more memory if declared inner class static, you'd insane optimize (use static makes sense, , need non-static, make non-static, don't convolute design save pointer of memory here or there).


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -