Why there will not be a default constructor in a class if explicit constructors with arguements are defined?
Because you don't always want a public, no-arguments constructor. If the compiler would always generate a public, no-args constructor, then it would be very hard to implement the singleton design pattern in Java, because a class couldn't control the creation of instances very easily.
You should look at it the other way around: The fact that the compiler generates a public, no-arguments constructor is a feature of the compiler to save you some typing. This is one of the many features that Java inherited from C++.
The java will not provide a defualt constructor If, any user defined constructor is present. The reason itself is present in above statement. Defining constructor means user want to declare a particluar type of object of that class.
Constructor is way to define the initial behavior of the object. you are defining the behavior. The whole object control is in your hand. Think of a situation, where you want to to declare a object which always takes two inputs in creation to decide its state, but some has created with out any input. The whole purpose of your class will go wrong.
So the constructors are used to define the initail behavior of the object.
So If you have atleast one way to define, then you are the control of the remaing ways too.
And If you do not provide any way ,then java will provide one way to set initial behavior to the object.
Hope, I have given enough information.
Please add if you know more ......
Joined: Oct 18, 2004
This makes me think that without default constructor I won't be able to instantiate the class using reflection. Right??
Then even if I don't intend to provide no argument constructor I need to provide one?
Note that a "default constructor" and a "niladic constructor" are different things. These terms are often erroneously used interchangeably as is the case in this thread. A default constructor is always implicit. An explicit no-argument constructor is not a default constructor. The default constructor's access modifier is always implied by some governing rules. An explicit no-argument constructor may have an explicit access modifier or an implied access modifier if none is provided (another common misconception is that this implication is always public).
Originally posted by Tony Morris: Note that a "default constructor" and a "niladic constructor" are different things. (...) An explicit no-argument constructor may have an explicit access modifier or an implied access modifier if none is provided (another common misconception is that this implication is always public).
Could you describe the rules that specify the access modifier for the default constructor and the implied case for the "niladic" (*) constructor?
(*) Had to look that up.
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Joined: Oct 18, 2004
Tony very useful piece of information ....
Partly to blame the so called bibles of Java do not make these things clear. It often in forums such as the ranch that makes one proficient.