Can we have return type for a constructor in java? Can we have private constructors in java?
Constructor should not have return type. Yes, we can have private constructors in java.
constructor can be private. Constructor will have return type.
constructor should be always public. Constructor should not have return type.
constructor can be both private or public. Some times constructors have return type.
Constructors should not have return type, as it won't return any value. But if you have return type for a constructor it will become a normal function, and not considered as a constructor.
It is not mandatory that a constructor should be public. We can have private constructors also, compiler won't give any error. We use private constructor to take the control of object creation with in the class.
It is heavily used especially in Singleton design pattern.
Back To Top