-1

What are advantages and disadvantages of the following options to create a file in Java that would only serve as a utility class:

  • interface with only static methods, vs.
  • final class with private constructor (or a public class with Lombok's @UtilityClass annotation) and the same static methods?
VytasZ
  • 1
  • 1
  • Downside of using an interface is that someone could implement it, which is not the intent. – Michael May 25 '22 at 10:49
  • I would go for the `final class` with a `private` constructor: a utility class with only - yes, indeed - utility methods should not be allowed to instantiate, nor extended, because that doesn't make sense. Hence `final` and `private`. – MC Emperor May 25 '22 at 10:57
  • [Don’t make utility classes.](https://stackoverflow.com/questions/40996614/whats-the-best-practice-for-creating-stateless-utility-classes-in-java/40999551) They’re usually the result of thinking like a C programmer. Put your static methods in the class they most pertain to. – VGR May 25 '22 at 17:24

0 Answers0