Classes are the bread and butter of Java applications. We write so many of them that sometimes we don’t even think of some basic yet fundamental truths. In this blog post we’ll take a closer look at the most important role of a class in Java.

Traditional way of thinking about classes

Class is one of the first concepts covered while learning object-oriented programming in almost all university courses, books, online tutorials etc. This is understandable because it is one of the most important concepts in software. But I’ve rarely, if ever, stumbled upon a text, an article, or a video which asks a seemingly obvious question: if this style of programming is called object-oriented and not class-oriented then how come we talk so much about classes and rarely about objects? I think that the reason so few people dared to ask this is a reason behind a traditional (and I’d dare to say wrong) way of thinking about classes. Let’s take a look at a few examples.

First one is from Brilliant:

In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
Karleigh Moore, Evans Njeru, Matas Pocevicius, and others

This is quite a typical way of explaining what a class is. Some keywords are blueprint for creating objects and state. Notice that behavior comes last. There’s no mention of how objects come to life.

Another one comes from Harvard University:

Class-based OOP In (pure) class-based OOP:

  1. Everything is an object
  2. Objects communicate via messages (handled by methods)
  3. Objects have their own state
  4. Every object is an instance of a class
  5. A class describes its instances’ behavior

While these points look much better they still put state pretty high in the list and behavior at the bottom. Also, no mention of how and why the transition between classes and objects happens.

The last example I’d share comes from the Cambridge University:

Classes can be seen as templates for representing various concepts We create instances of classes in a similar way. An instance of a class is called an object

Instead of a blueprint they use template but the meaning is, I believe, the same.

To really understand the most important job a class must do, we first must quickly analyze two phases of execution of every object-oriented application ever written.

How every object-oriented application executes

Execution of every single object-oriented application has two phases:

  1. Constructing and wiring (connecting) objects together
  2. Telling one object to start which effectively starts the application

Those phases are fundamentally different and this difference is crucial to understanding object orientation. Think of the first phase as the preparation. In it we have the goal of building the interconnected network of components which would do the actual work in the second phase. Nothing else should happen here - no business logic, no responding to user input, no fetching data from the DB… These interconnected components are our objects! Constructing and wiring objects

It’s of utmost importance that we create the right objects but also connect them properly with one another. You can say this is like assembling the machine out of its parts. If we fail to do so our application will surely fail to execute. Think of the second phase then as execution in which we start the machine and let it run. All the interesting stuff happens here - the application reacts to user input, processes some data based on business rules, communicates with external systems… Objects which we have created and connected in the first phase now work tirelessly, collaborating with one another as autonomous agents, sending messages to each other and reacting to those messages. This continues until the application’s job is done and it stops executing.

The most important task of a class is therefore…

I hope it’s clear now that the most important task of a class is to always create an object in a valid state and properly connected to its collaborators. Classes do that through special methods we call constructors. As a corollary we can say that constructors are the most important part of the class to implement correctly.

Constructors are a portal between the static world of classes and the dynamic world of objects. After executing them, our application has created all the entities it needs to become alive and let objects interact with each other in this newly populated world. If something wrong happens in this phase our application would be stillborn. Therefore we must pay due attention to make this right.

Dear fellow developer, thank you for reading this article about the most important role of a class in Java. Until next time, TheJavaGuy salutes you 👋!