I have an Outer
class which has a private Inner
class.
In my Outer
class method, I instantiate the Inner
class as follows:
Outer outer = new Outer();
Inner inner = outer.new Inner();
The compiler converts this code to:
Outer outer = new Outer();
Inner inner = new Inner(outer, null);
Using reflection shows that the Inner
class has the following synthesized constructors:
private Outer$Inner(Outer)
Outer$Inner(Outer,Outer$Inner)
Since the Inner
class is private
, the compiler adds that private
constructor to it so nobody can instantiate that class. But obviously the Outer
class should be able to instantiate it, so the compiler adds that other package private constructor which in turn calls the private constructor. Also, since the package-private constructor has that $
in its name, normal Java code can't call it.
Question: why synthesize one private and one package-private constructor? Why not synthesize only the package-private constructor and be done with it?