0% found this document useful (0 votes)
56 views

Anonymous Classes in Java

This document discusses anonymous classes in Java. Anonymous classes allow for the inline definition of a class without a name that can override methods. While initially seeming appealing, anonymous classes often lead to code duplication and decreased readability and debugability over time. The document recommends only using anonymous classes sparingly, if at all, and refactoring them into named classes when the same implementation is needed in multiple places.

Uploaded by

NikolaMilosevic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Anonymous Classes in Java

This document discusses anonymous classes in Java. Anonymous classes allow for the inline definition of a class without a name that can override methods. While initially seeming appealing, anonymous classes often lead to code duplication and decreased readability and debugability over time. The document recommends only using anonymous classes sparingly, if at all, and refactoring them into named classes when the same implementation is needed in multiple places.

Uploaded by

NikolaMilosevic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Sign in Get started

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.

Anonymous classes in Java


F or 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 Follow
Jul 15 · 3 min read

1 //Note now I saved this anonymous class in a variable


2 //but you could just define a new AnonymousClass inline inside another function that expe
3 AnonymousClass anonClass = new AnonymousClass() {
4 @Override
5 public void onExample(){
6 //set specific code in here.
7 }
8 }

anonymousClass.java hosted with ❤ by GitHub view raw

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.

Programming Java Anonymous Class Programming T ips Learning To Code

23 claps 1 response

WRIT T EN BY

Friedel Verpoort Follow

Fill his days as a freelance game developer. Also loves writing


from time to time and generally enjoys life.

Javarevisited Follow

An humble place to learn Java and Programming better.

More From Medium

T he quirks of Implementing C/C++ TCP/IP Socket How We Found Apache


programming music Dynamic Shared Programming in Java Tomcat Couldn’t Handle
theory Libraries with Teradata Prayukti Jain in T he Startup Large Requests
Nick Rose UDFs Stefan Pfa el in Javarevisited
Tony Tannous

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

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. On Medium, Follow all the topics you care about, and we’ll deliver the Get unlimited access to the best stories on Medium — and
smart voices and original ideas take center stage - with no best stories for you to your homepage and inbox. Explore support writers while you’re at it. Just $5/month. Upgrade
ads in sight. Watch

About Help Legal

You might also like