For me the best explanation is in Effective Java book: Item 4: Enforce noninstantiability with a private constructor (See more)
In Summary:
- Private constructor is due utility classes were not designed to be instantiated, so is a design decision. (NO performance or memory overhead)
- Making a class abstract doesn't work because can be subclassed and then instantiated.
- With an abstract class the user may think the class is for inheritance.
- The only way to ensure no instantiation is to add a
private constructor
which ensures the default constructor is not generated. - Private constructor prevents inheritance because the super constructor cannot be called (so it is not need the declare the class as final)
- Throw an error in the private constructor avoids call it within the class.
Definetively, the best way would be something like next:
public class MyStrings {
private MyStrings () {
throw new AssertionError();
}
...
}