Blog

The fear of classes is real—Here’s how to beat it in Java

By  
Matti Tahvonen
Matti Tahvonen
·
On Dec 5, 2024 6:58:45 PM
·

“Java is a high-level, class-based, object-oriented programming language” - Wikipedia.

I get to see and read a lot of Java code at work. Sometimes, I even get to write some.🥳 Recently, I have noticed a tendency that we Java developers are, for some (wrong?) reason, afraid of classes. A class is a fundamental building block of quality Java code. By instantiating new objects, we are constantly utilizing classes created by others, but we often forget that we also have the privilege to define them.

Two screenshots comparing the same code written with and without classes - both without reading glasses (blurred).

Two screenshots comparing the same code written with and without classes - both without reading glasses (blurred).

The purpose of classes

Perhaps it’s due to laziness or the misconception that creating new classes negatively impacts performance. Modern IDEs, however, can effortlessly generate those “extra lines” of code for you, and the JVM is built to handle numerous classes with ease. While simplifying code by avoiding classes may seem "smart," it’s often just another example of the common developer pitfall: premature optimization.

Recently, I’ve been working to cure my own “fear of classes” by reminding myself that reuse is only a secondary benefit of using classes. The primary purpose of classes isn’t just to minimize code through reuse but to bring structure to our code.

Classes force us to think about the naming and responsibilities of different parts of our code. This improved structure saves time and conserves our (limited) mental energy when revisiting the code later. And trust me, you—or at least someone else—will return to it eventually.

Another thing that has helped me has been to allow myself to use inner classes. A .java file per class is the de-facto standard for Java developers, but especially if writing application-specific UI code with a platform like Vaadin, I think inner classes, even non-static ones, are just fine! They already help you (e.g., IDE in auto-formatting your code) to give much-needed structure to your code. They will save you time and money. If you (later) figure out a place for reuse, you are also one step closer to accomplishing that. Your IDE will help you to “promote them” to be top-level classes.

Tips to overcome the fear of classes

Here are my tips for overcoming the “fear of classes.” Can you think of other good ones? I’d love to expand the list!

  • Focus on maintainability: Remember that the main purpose of classes is to improve maintainability by providing structure and clarity—not just to enable reuse or save lines of code.
  • Check if your code lacks structure: if you can’t easily see the code’s structure (e.g., without your reading glasses 👓 or from a distance), you probably need to create a class.
  • Look for repeated method calls: if you’re repeatedly calling the same object’s methods, consider extracting a class.
  • Notice repeated configurations: if multiple objects in the same class share similar configurations, even if it is only a single method call per object, it’s time for a class.
  • Make classes “lightweight” if needed:
    • Use inner classes to save an extra file and reduce imports; you can always refactor them to top-level later.
    • Start with inline classes if you struggle to name a new “thing” right away. This will already provide structure and help with auto-formatting.
    • Use records where appropriate, even within your current class.

For more inspiration, take a look at my recent coding exercise and individual commits, where I created the featured image for this article.

<NerdHumor> I have had this article in my mind for quite a long time. I finally got it started when I was listening to a power song that I often used years ago when doing some night orienteering (endurance sport). Fear of the Dark by Iron Maiden still helps me focus sometimes. The era of LLMs and my good friend ChatGPT inspired me to “produce” new lyrics for a well-known rock classic. 😁 

Fear of the Class

(to the tune of Iron Maiden's "Fear of the Dark")

Verse 1
I am a coder who walks in the dark,
And in Java code I leave my mark.
But something whispers, says it won’t last,
When I shy away from building a class.

There’s this feeling I get deep inside,
That my methods are messier than I implied.
And I break my code apart piece by piece,
But these scattered methods won’t bring me peace.

Chorus
Fear of the class, fear of the class!
Oh-oh-oh-oh!
It’s creeping in on every pass.
Fear of the class!

Verse 2
A glimpse of code that's over my head,
Where naming’s unclear and structure’s dead.
Could this all just be laziness,
When I know classes bring order, less stress?

Maybe I think I’m smarter than I am,
"Keep it simple, avoid the RAM!"
But Java’s built to handle the strain,
Classes keep my code from being in vain.

Chorus
Fear of the class, fear of the class!
Oh-oh-oh-oh!
The JVM’s power will surely outlast,
Fear of the class!

Bridge
If I don’t create structure, I’ll just create pain,
A tangled web I won’t want to maintain.
Forget the shortcuts, it’s time to decide,
That clarity’s key, so I’ll code with pride.

Verse 3
Inner classes to start, so tidy and tight,
With the IDE helping me see the light.
I’ll name them well, I’ll give them their place,
So I can return without hiding my face.

Outro
Fear of the class, fear of the class!
Oh-oh-oh-oh!
I’ve broken free, and I’ll raise a glass,
To conquering fear of the class!

Share your thoughts

These are my tips and insights on embracing classes in Java. Have you encountered similar challenges or found other effective strategies to overcome the "fear of classes"?

Let’s expand the list together—drop your ideas in the comments or join the conversation on the Vaadin Forum!

Matti Tahvonen
Matti Tahvonen
Matti Tahvonen has a long history in Vaadin R&D: developing the core framework from the dark ages of pure JS client side to the GWT era and creating number of official and unofficial Vaadin add-ons. His current responsibility is to keep you up to date with latest and greatest Vaadin related technologies. You can follow him on Twitter – @MattiTahvonen
Other posts by Matti Tahvonen