Anonymous Classes in Java
Anonymous Classes in Java
Javarevisited T HINGS T O LEARN GET P UBLIS HED JAVA P ROGRAMMING RES OURCE ARCHIVE BLOG
You have 2 free stories left this month. Sign up and get an extra one for free.
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.
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.
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.
23 claps 1 response
WRIT T EN BY
Javarevisited Follow
How to Set Up a CI/CD Python Errors Done A Practical Guide to How To Use Flask-
Pipeline for AWS Lambda Right Azure Durable Functions WT Forms
With GitHub Actions and Ilia Zaitsev in T he Startup —Part 2: Dependency Qiang Hao in Better
Serverless Injection Programming
Philipp Schmid in Better Allen Zhang in T he Startup
Programming