title: Java Classes: Anonymous vs Nested vs Private
body: <p>Can someone explain the difference between anonymous classes, nested classes and private classes in Java? I'd like to know the runtime costs associated with each and the compiler approach to each, so I can get a grip on which is best to use for e.g. performance (potential for compiler optimisations), memory usage, and general acceptability with other Java coders.</p>
  
  <p>By anonymous classes I mean those declared inline in a function in that weird Java way. This is often seen in examples of Swing code where you write a button handler inline with the declaration of it. I don't really like anonymous classes because you get those funny MyClass$1.class files and more often than not you'll have to refactor later when it turns out you want to make the anonymous class a singleton or need a handle to it or something. I also just don't like the way you're attaching a handler in the form of something you just made up whilst in the middle of another function call. It just makes me feel weird. :-)</p>
  
  <p>Private classes are the ones you write at the bottom of your file completely outside the declaration of your public class. I prefer private classes just because they make sense in other languages where there isn't a strong 1:1 relationship between a source file and a class. I don't get that weird feeling and I know (or I think I know) how a Java compiler will handle the declaration of that class.</p>
  
  <p>Nested classes are ones written inside the squigglies of your public class. I really don't know what a nested class means compared to the other two. :-)</p>