Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a utility class be static? [closed]

Tags:

java

If I have to design a Utility class( such as ByteUtils or StreamUtils or StringUtils), what is the best design choice for them.

  • Should they be static classes (as I won't have any states to store)
  • Should they be non-static classes ( so that if the objects are not used, they will be gc'd)

PS: By static class, I meant a class with static methods(and not the inner static class)

Please give advice on design choices for this ?

like image 860
nibin012 Avatar asked Aug 16 '11 07:08

nibin012


People also ask

Are utilities static?

Utility Classes are classes that have only static methods that perform some operation on the objects passed as parameters. Such classes typically have no state. Utility Class is usually declared final, so it cannot be subclassed.

Can Util class have non static methods?

Utility class methods MUST NOT have dependencies to non static methods like other class instances or global variables.

When should a class be made static?

A class can be declared static only if it is a nested class. It does not require any reference of the outer class. The property of the static class is that it does not allows us to access the non-static members of the outer class.

Why are static methods useful in creating utility classes?

In Java programming, the main motivation for making a method static is convenience. You can call a static method without creating any object, just by using its class name. So if you need a method, which you want to call directly by class name, make that method static. Utility classes e.g. java.


3 Answers

My utility classes look like this:

// final, because it's not supposed to be subclassed
public final class FooUtil {

    // private constructor to avoid unnecessary instantiation of the class
    private FooUtil() {
    }

    public static int doSomethingUseful() {
    }

    // ...
}

Note that, although this makes the utility methods easily testable, and easily accessible from the outside, it also makes the classes using them hard to unit-test, because it's not easy to mock these utility methods. Having too many of such utility classes can be a sign of a lack of OO design (procedural programming), and can really make the code hard to test.

If you're using a dependency injection framework (Spring, Guice, whatever), it might be a good idea to just make the utility class instantiatable, with non-static methods, and make it an injectable singleton. This way, classes using these utility methods can be tested by mocking the utility object.

like image 80
JB Nizet Avatar answered Oct 19 '22 08:10

JB Nizet


If it is a general purpose utility, static is IMO better. You stated that you will not have any states to store, so I don't see why should you make it non-static. Declaring it to be static will save memory too.

like image 50
Gapton Avatar answered Oct 19 '22 07:10

Gapton


Just because something can be static, doesn't mean it should be static.

There is another consideration to all this: mocking. It's harder to mock static methods in your tests than it is to mock the behaviour of an instance of a class.

Talk of unnecessary heap allocations and GCing of objects smacks of premature optimisation to me. The JVM will do a pretty good job at optimising away this sort of issue.

like image 24
mrbaboo Avatar answered Oct 19 '22 07:10

mrbaboo