Anonymous classes in Java

For the people that aren’t aware what anonymous classes are, let me give you a quick rundown. An anonymous class is an inline class without a name, that overloads methods. This happens through a constructor-like creation process… everything will become clear with the example below.

Friedel Verpoort
Published in
3 min readJul 15, 2020

--

Yes, this is a rather unknown language-feature that isn’t found in many other programming languages. Besides Java, I am only aware of PHP7 having this language feature.

When I first learned about the feature I didn’t see the appeal of using it, but soon after I worked on a project that was using this technique when overwriting small interface classes.

Being able to do this inline was a quick an easy win in the short term. In the long-term however you will most likely need the exact same implementation somewhere else in your code. That is when the question arose, either you have the same anonymous class in two places of your codebase or you have to create a small class for it instead.

Tim Gouw — Pexels

Obviously the best choice is to create a small named-class instead. When I looked through the project though, I could spot the exact same classes scattered around though. The reasons behind this were either:

  • due to the lack of time or deadlines, so these anonymous classes were often copy-pasted across the code.
  • Another reason was because the person was just unaware of the other place in code this was implemented and spent the time to implement it again.

This led to an increase of bugs because the implementations became slightly different or were overlooked when implementing some new functionality. It is better to create a rigid subclass/implementation of the feature instead of having anonymous classes scattered around in code. Having them in a proper class also makes the debugability and readability of the code significantly better.

However neat the idea is of having small inline classes it is not worth the short-term ease, over the long term flexibility.

Why would you use anonymous classes?

Using anonymous subclasses also feels very unnecessary as most of the time you can replace this feature with lambdas, delegates or events in Java or other languages. This became abundantly clear when the decision was made to move the whole Java codebase over to C# and we finally got rid of all these smaller inline anonymous classes.

Conclusion

I would suggest staying away from anonymous classes as much as in your projects, however, I can see the appeal of using it in specific cases. I would however suggest to not use them in the beginning of a new project and only start using them once your project matures. You can also use them to interface with plugins and singletons in libGDX, which I’ve seen first-hand as well. I think that is totally up to the programmer, but I learned my lesson and will always be cautious about this language feature.

Cover image by Batu Gezer on Unsplash.

--

--

Friedel Verpoort
Javarevisited

Fill his days as a freelance game developer. Also loves writing from time to time and generally enjoys life.