Jave inner classes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Jave inner classes

copied from https://old.reddit.com/r/GoodSoftware/comments/d3glp4/jave_inner_classes/

See Inner.java

The Inner instance isn't garbage collected here because all inner classes keep a reference to the outer class. This is horrible, especially for anonymous inner classes that don't user the other class at all. I thought the Java compiler would have the intelligence to detect this for anonymous inner classes and to make them effectively static when it has no reference to the outer class, but no. This can cause serious garbage collection issues.

Of course I don't use most new Java features. But inner classes were released early, so here we begin to see where Java went wrong. Java is supposed be like an object-oriented C with garbage collection. This means that it should be explicit with no magic and no hidden nonsense. Inner classes were the first violation of Java's initial good design, with a hidden reference to the outer class. Not only does this cause garbage collection issues but it also breaks the clone() method. Java should have only added static nested classes. Anonymous "inner" classes should have also been static and should have only had access to outer variables and the outer class during construction. This would force things to be explicit and would eliminate all problems and would have been consistent with what Java was originally meant to be.

Of course as modern culture became depraved, all kinds of insane depraved features were added to Java. For example lambda expressions which are really depraved pseudo-closures implemented like broken anonymous inner classes. Of course I never use such depravity which was designed by and for modern scum. True closures make sense for languages based on this concept like Luan is. True closures work because they only keep references to variables that they really use. But closures don't belong in a language like Java.

In conclusion, don't use inner classes at all. Only use static nested classes. And avoid most new Java features. I will write a full review of Java when I have time, explaining which features are good or bad.