I would say you’re doing it right. Apart of that, some advices for your utility class:
- Make sure it doesn’t have any state. This is, there’s no field in the class unless it’s declared
static final. Also, make sure this field is immutable as well e.g.Strings. - Make sure it cannot be a super class of other classes. Make the class
finalso other programmers cannot extend it. - This one is debatable, but you may declare a no-arg constructor
private, so no other class could create an instance of your utility class (using reflection or something similar will do, but there’s no need to go that protective with the class). Why you may not do this? Well, this is the strange case where you want/need to inject an instance of the utility class e.g. through an interface rather than directly using it along your class. Here’s an example of this. This design is really odd but may happen (as shown in the link above), but if you will not run in such case, the best advice is to keep the constructorprivate.
There are lot of libraries that provide utility classes in order to help us programmers with our work. One of the most known is Apache Common set of libraries. It’s open source and you can check the code to see how they design these utility classes in order to create yours. (DISCLAIMER: I do not work or support these libraries, I’m a happy user of them)
Important Note: Avoid using a singleton for your utility class.