I want to create a class with utility methods, for example
public class Util { public static void f (int i) {...} public static int g (int i, int j) {...} }
Which is the best method to create an utility class?
Should I use a private constructor?
Should I make the utility class for abstract class?
Should I do nothing?
Methods in the class should have appropriate names. Methods only used by the class itself should be private. The class should not have any non-final/non-static class fields. The class can also be statically imported by other classes to improve code readability (this depends on the complexity of the project however).
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. lang.
A utility class is a class that is just a namespace for functions. No instances of it can exist, and all its members are static. For example, java. lang. Math and java.
Utility Class (. NET Micro Framework) provides a collection of helper functions you can use to configure settings for security, collections, driver manipulation, time, and idle CPU usage.
For a completely stateless utility class in Java, I suggest the class be declared public
and final
, and have a private constructor to prevent instantiation. The final
keyword prevents sub-classing and can improve efficiency at runtime.
The class should contain all static
methods and should not be declared abstract
(as that would imply the class is not concrete and has to be implemented in some way).
The class should be given a name that corresponds to its set of provided utilities (or "Util" if the class is to provide a wide range of uncategorized utilities).
The class should not contain a nested class unless the nested class is to be a utility class as well (though this practice is potentially complex and hurts readability).
Methods in the class should have appropriate names.
Methods only used by the class itself should be private.
The class should not have any non-final/non-static class fields.
The class can also be statically imported by other classes to improve code readability (this depends on the complexity of the project however).
Example:
public final class ExampleUtilities { // Example Utility method public static int foo(int i, int j) { int val; //Do stuff return val; } // Example Utility method overloaded public static float foo(float i, float j) { float val; //Do stuff return val; } // Example Utility method calling private method public static long bar(int p) { return hid(p) * hid(p); } // Example private method private static long hid(int i) { return i * 2 + 1; } }
Perhaps most importantly of all, the documentation for each method should be precise and descriptive. Chances are methods from this class will be used very often and its good to have high quality documentation to complement the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With