Utility class/method

A utility class is a helper class with static variables and static methods that perform a specific list of related tasks.

Let's look at examples of standard utility classes:

java.lang.Math This class, which can perform many different mathematical calculations, gives us some mathematical constants.
java.util.Arrays The class contains various methods for working with arrays (such as sorting and searching them). This class also has a static factory that lets us view arrays as lists.
java.lang.System This class implements methods for working with the system. Most often we use it to display text on the console. To do this, we reference the static out variable, which stores a PrintStream object, and call its println method (System.out.println).

We can also create a utility class ourselves: to do this, we simply create a class with the static public methods that we need. But remember that you should have a good reason for creating a utility class. For example, perhaps you need to use the same method or set of methods to perform a single task (such as a complex calculation) in several different classes.

Let's look at an example of a utility class — the Paths class.

Paths class

This class consists of just one static get method that has two variants with different parameter lists.

The arguments that we can pass to the get method are:

get(String first, String... more) A whole path or a list of names of directories and (or) the file in the last argument.
get(URI uri) A URI.

This utility class solves the problem of converting a path (in the form of a string) or URI to a Path. We've already explored Path and understand why we need it and how we can work with it.

As it happens, we often deal with paths in the form of Strings or URIs. This is where we can use the methods of the Paths utility class.

Let's look at examples:

Example Comment

Path path =
Paths.get("C:\\Users\\User\\Documents\\MyFile.txt");
                    
We pass the get method a String (the path to the file) and get a Path. Then we can work with it as needed.

Path path = Paths.get(URI.create("file:///Users/User/Code/MyClass.java"));
                    
A Path can also be obtained from a URI.

Path path = Paths.get(System.getProperty("user.home"),"documents", "document.txt");
                    
We indicate the sequence of directory names and the name of the file whose path with need.

But there is a caveat here. With the arrival of Java 11, any implementation of the get method calls Path.of.


public static Path get(String first, String... more) {
    return Path.of(first, more);
}
 
public static Path get(URI uri) {
    return Path.of(uri);
}
    

This utility class may be declared deprecated, so we should use Path.of instead.

Before After

Path path =
Paths.get("C:\\Users\\User\\Documents\\MyFile.txt");
                    

Path path =
Path.of("C:\\Users\\User\\Documents\\MyFile.txt");
                    

Path path = Paths.get(URI.create("file:///Users/User/Code/MyClass.java"));
                    

Path path = Path.of(URI.create("file:///Users/User/Code/MyClass.java"));
                    

Path path = Paths.get(System.getProperty("user.home"),"documents", "document.txt");
                    

ath path = Path.of(System.getProperty("user.home"),"documents", "document.txt");