Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an utility class? [duplicate]

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?

like image 282
user2693979 Avatar asked Aug 09 '14 22:08

user2693979


People also ask

How do you create a utility class in java?

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).

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. lang.

What is a utility class?

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.

What is utility class in C#?

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.


1 Answers

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.

like image 131
initramfs Avatar answered Sep 21 '22 15:09

initramfs