-1

https://stackoverflow.com/a/7486111/17273668 ; from what I have seen here to make a class "static" , we have to make it final with private constructor and static fields and methods.

Is there any difference between making the constructor private and making the class abstract?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    A static class in Java is something else entirely. Please don't try to import terminology from another language (e.g. C#) to a different language where that terminology means something else entirely. – Mark Rotteveel Dec 04 '22 at 10:25

2 Answers2

3

There is a huge difference between making a constructor private, or making a class abstract. Making a constructor private means that the constructor can only be invoked within the class itself or its nested classes, which means it cannot be called from outside. Making a class abstract means that it can only be instantiated by subclassing it. If a class is abstract, but has a non-private constructor (the default constructor is public), it means it can be subclassed by classes in other compilation units.

When it comes to utility classes, making the class final with a private constructor is - in my opinion - the better choice. The alternative is to make it abstract with a private constructor, but I think that is abusing the term abstract. Making something abstract raises an expectation of having concrete subclasses, which is not what you do with a utility class.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • What if the class is final abstract ? is it similar to final class with private constructor ? – Dhia Ben Aicha Dec 04 '22 at 10:38
  • @DhiaBenAicha A class cannot be final and abstract at the same time. – Mark Rotteveel Dec 04 '22 at 10:39
  • @user16320675 That talks about methods, not about classes, you probably want to quote [JLS 8.1.1.1](https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.1.1.1) instead: _"It is a compile-time error to declare an abstract class type such that it is not possible to create a subclass that implements all of its abstract methods. "_ – Mark Rotteveel Dec 04 '22 at 10:42
2

An abstract class can be extended by sub classes, a private constructor (if it is the only constructor) prevents sub-classing (exception: nested classes). The only way to instantiate a class with private constructor is by implementing a static factory method in the class itself (e.g. Optional.of).

knittl
  • 246,190
  • 53
  • 318
  • 364
  • A private constructor does not prevent subclassing by nested classes. – Mark Rotteveel Dec 04 '22 at 10:31
  • @DhiaBenAicha A class cannot be final and abstract at the same time. – Mark Rotteveel Dec 04 '22 at 10:38
  • 1
    `final abstract` doesn't really make sense. `abstract` means it must be extended to be instantiable, `final` means it cannot be extended. It's not possible to combine both modifiers for a single class. – knittl Dec 04 '22 at 10:39
  • 2
    Attempting to compile a `final abstract class` results in a compilation error: _"java: illegal combination of modifiers: abstract and final"_ – Mark Rotteveel Dec 04 '22 at 10:40