Java Lombok | Utility Objects - @UtilityClass

Utility Objects

When you use @UtilityClass, Lombok makes the constructor private and makes all methods static.

The code below is an example that uses @UtilityClass.

package com.devkuma.tutorial.lombok;

import lombok.experimental.UtilityClass;

@UtilityClass
public class UtilityClassTutorial {

    public int plus(int a, int b) {
        return a + b;
    }
}

Because of @UtilityClass, the code above is transformed as follows.

package com.devkuma.tutorial.lombok;

public final class UtilityClassTutorial {
    public static int plus(int a, int b) {
        return a + b;
    }

    private UtilityClassTutorial() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }
}