Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should the declaration of a helper class, with only static methods, be?

I've got a class here, that only contains static methods, so I call it a helper class, an example:

public class FileUtils {
    public static String readFileEntry(final Path path, final String entryKey) throws IOException { }

    public static String readAndModifyFileEntry(final Path path, final String entryKey, final UnaryOperator<String> operator) throws IOException { }
}

How should I declare the class (abstract, final, static, etc.), such that it is not possible to instantiate the class? As you are not supposed to do so.
If that is not possible, then what is the best practice?

I'm using Java 8 if that is of any extra help.

like image 823
skiwi Avatar asked Dec 20 '22 16:12

skiwi


1 Answers

You can declare the constructor as private and use the final keyword to prevent extensions :

public final class FileUtils {
    private FileUtils() {
    }

    public static String readFileEntry(final Path path, final String entryKey) throws IOException { }

    public static String readAndModifyFileEntry(final Path path, final String entryKey, final UnaryOperator<String> operator) throws IOException { }
}
like image 180
giorashc Avatar answered Dec 22 '22 05:12

giorashc