How to Design Utility Classes in Java?

mammadyahyayev

Mammad Yahyayev

Posted on July 8, 2024

How to Design Utility Classes in Java?

NOTE: This article had been written in 2021.

Hello everybody, Today we gonna talk about utility or helper class. I will answer What is that ? , Why we need that ?. Before the deep dive in Utility class implementation in Java , first we should understand what is that.

What is Utility Class?

The utility class is helper. Let's say we have a little logic in the code and we use this logic throughout the project, instead of writing this logic over and over in each class, we can create a new class and add this logic to the newly created class. And then we can call this logic wherever we want. Maybe we have 10 lines of code in our logic, we can replace this 10 lines of code with 1 line.

Why we need Utility Class?

Utility class helps us to improve code quality , readability. Also you can use Utility class to write good clean code. Other developers can understand your code easily. This is the main idea to use this class everywhere in the project. Now let's see this Utility class in action.

Implement Utility class to our Project

First of All we use IDE (Integrated Development Environment). IDE makes our life easy. I am using Intellij Idea because this IDE is very powerful and very popular among the Java developers. Of course you can use any IDE what you like.
Open your favorite IDE and create new Java project. And then create a new class, this will be Main Class. Inside of this Main class create a main method and start to write own logic.
Let's assume we want to convert Date format to our format what we like. For doing this create Calendar instance, Why calendar? because some of Date methods are deprecated. Now focus on the our logic. First of all we create instance of Calendar with Calendar.getInstance().



Calendar calendar = Calendar.getInstance();


Enter fullscreen mode Exit fullscreen mode

If i print this date we use following code.



System.out.println(calendar.getTime());


Enter fullscreen mode Exit fullscreen mode

calendar.getTime() method gave us Date object. If i run the Main class output will be this Sun Jan 03 12:14:54 AZT 2021. But I want to see this date in the format (03–01–2021 - dd-MM-yyyy). Now let's write the logic.



SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = dateFormat.format(calendar.getTime());


Enter fullscreen mode Exit fullscreen mode

In this logic I create instance of SimpleDateFormat and pass our pattern to this constructor of SimpleDateFormat. And then I call format() method of SimpleDateFormat and this method takes date object in our case we pass calendar.getTime() because this method gave us the Date object and then I assign the new value to this format method.

This format method returns back String object thus I assign String object and give formattedDate name to this String object. Now print this formattedDate object and see the result.

Result will be this 03–01–2021. In this case we wrote 4 lines of code, i know this is not much but if you use this logic in each class, your code will increase.

Now let's create Utility class and add this logic to this class. You can give any name you like but i give DateUtils. This make sense because this logic related to Date. In this class we create a method and this method returns String object.

parseDate() method

Now call this method in the Main Class.

execution of utility method

As you can see, we instantiate this class and print the parseDate() method, the result will be the same as before.

Now let's design our Utility class, first we need to know that such classes contain static final fields and static methods. In Java, we can call the static method by simply typing Classname.methodName.

In our code, we can call this DateUtils.parseDate() method after making the method static. This class only contains static methods we don't need to instantiate hence we can change this class to abstract class , In Java we can not instantiate abstract class.

utility abstract class

Now create static final fields and assign pattern to that field.

static final field

This is the end. Now in our simple project we replace 4 lines of code with 1 line and also we can use this logic in everywhere, just simply call DateUtils.parseDate().

Also code is available on the Github.

Conclusion

In this blog post we have seen how to implement and design the Utility Class. Maybe this project is simple but you should use this class in a big project and it will make your life easier.

Thank you for reading this article and I hope you try this on your project.

Feel free to reach me via LinkedIn

Hope to see you in other articles. Bye-Bye.

💖 💪 🙅 🚩
mammadyahyayev
Mammad Yahyayev

Posted on July 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related