Are there any use cases for .new that cannot be handled by refactoring the inner class to the outer class?
There are usually no important use cases in this syntax since you can always hide the inner constructor and let the outer class return instances of the inner class using factory methods (if you intend to divulge the inner type in appearance anyway)
public class Outer { public class Inner { private Inner() {} } public Inner createInner() { return new Inner(); } }
Client Code:
Outer outer = new Outer(); Inner inner = outer.createInner();
Thus, the "smell of code" (if it is really the smell of code) is not that the inner class is present, but that its constructor is public (and used). But after a thorough assessment, you can see that this is normal
UPDATE A good sign to indicate that encapsulation is broken is when Outer instances probably have a shorter lifespan than Inner instances. In this case, Inner instances may contain a reference to Outer for a longer period of time than intended by the author of Outer . This can even lead to memory leaks.
Lukas Eder
source share