Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: prefer Utility Class depending on a class instance or static methods?

I've a Java class that does something like this

public class MyObjectUtil {

    private final MyObject myObject;

    public MyObjectUtil (MyObject myObject){            
        this.myObject = myObject;    
    }

    public boolean isSomeInfoPresentValid() {
        return myObject.isSomething();
    }

    public void modifyMyObjectInSomeWay(String blah) {
        myObject.modify(blah);
    }

}

Every time I've to instantiate my Utility class to interact with a specific instance of MyObject.

myObject is present as a Session obj

MyObjectUtil myObjUtil = new MyObjectUtil(myObject);
myObjUtil.modifyMyObjectInSomeWay("blah");
boolean valid = myObjUtil.isSomeInfoPresentValid();

To achieve the same result I could have static functions that do the same operations using myObject every time as a method param.

public class MyObjectUtil {

    public static boolean isSomeInfoPresentValid(MyObject myObject) {
        return myObject.isSomething();
    }

    public static void modifyMyObjectInSomeWay(MyObject myObject, String blah) {
        myObject.modify(blah);
    }

}

I don't know which way I should go.
I'm using the Util class in my controllers and sometimes I need to call few methods against the same myObject instance, so to me the 1st solution seems the more correct. At the same time in this way as I've many request against my controllers I can end up creating a lot of MyObjectUtil instances everytime as myObject is related to a specific http request.

Which way should I go in my case and how should I choose in other cases?

MyObject is used in specific ways by some Utilities Classes like MyObjectXXXUtil MyObjectYYYUtil. I would like to mantain these Util methods (that modify myObject and check specific status) out of the specific MyObject implementation, as they are not specific to that. Many Util functions can interact with MyObject in a certain way.

like image 305
spike07 Avatar asked Dec 13 '10 11:12

spike07


People also ask

Should utility classes be static Java?

Pure utility classes should usually be static. When you have a class with well-defined input and output, no side effects and no state, then by definition it should be a static class.

What type of methods are used by a utility class?

Utility Class, also known as Helper class, is a class, which contains just static methods, it is stateless and cannot be instantiated. It contains a bunch of related methods, so they can be reused across the application. As an example consider Apache StringUtils, CollectionUtils or java.

When should you create a utility class?

Whenever a common block of code needs to be used from multiple places, we can create utils class. Example: I want to verify whether a text is null or empty. This will be used in several places in my project.


2 Answers

If the methods in MyObjectUtil are completely dependent on MyObject then I would just put them in MyObject. A utility class is useful when you have a lot of classes that can use the methods.

like image 174
JOTN Avatar answered Sep 22 '22 00:09

JOTN


Here are some other threads that have discussed this before:

Java Utility Class vs. Service

and

Java Abstract Class or Static Utility Class Design Choice

For me it all depends on state. If the utility class doesn't need state then I go for the static methodology.

Also the methods you describe could arguably exist on the object itself as valid actions to perform on that domain object.

like image 27
Martijn Verburg Avatar answered Sep 18 '22 00:09

Martijn Verburg