Can someone explain the difference between anonymous classes, nested classes, and private classes in Java? I would like to know the runtime costs associated with each of them, and the approach to the compiler for each, so I can access which is best used, for example. performance (potential for compiler optimization), memory usage, and general acceptability with other Java codes.
By anonymous classes, I mean those that are declared inline in a function in this strange Java mode. This is often found in Swing code examples, where you write a button handler inside with a declaration. I donโt like anonymous classes because you get these funny MyClass $ 1.class files, and most of the time you will have to reorganize later when it turns out that you want to make the anonymous class a single or if you need a handle or something. I also don't like the way you attach a handler in the form of something that you just created, while in the middle of another function call. It just makes me feel weird. :-)
Private classes are those that you write at the bottom of the file completely outside the declaration of your public class. I prefer private classes only because they make sense in other languages โโwhere there is no 1: 1 strong connection between the source file and the class. I donโt feel this strange feeling, and I know (or, it seems to me, know) how the Java compiler will handle the declaration of this class.
Nested classes are those written inside the squigglies of your public class. I really don't know what a nested class means compared to the other two. :-)
user595447
source share