Answer the question
In order to leave comments, you need to log in
Why do finalized classes in java sources have private constructors?
In the course of learning the language, I discovered the source codes and plunged headlong into them. Now I'm looking at the Math class and StrictMath. I noticed that both (and many others) have a private constructor and an interesting comment.
/*
* Don't let anyone instantiate this class.
*/
private Math() {}
Answer the question
In order to leave comments, you need to log in
Because these classes are not classes in general. They are made classes solely due to the fact that in Java you cannot write any code outside the class. These utility classes are essentially namespaces for pure functions (static methods). It is pointless to create their instances, and therefore it is forbidden.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question