Why default constructor is not provided by compiler when class contains parametrized constructor defined by user?

347 Views Asked by At

I am a newbie in java and was wondering "Why default constructor is not provided by compiler when class contains parametrized constructor defined by user?"

1

There are 1 best solutions below

0
GhostCat On

When an author decides to not provide any constructor, it is perfectly fine that the compiler adds that default constructor. Obviously the user doesn't care "how" objects of that class are created, he accepts that the "default" kicks in.

But as soon as the author writes down a constructor with parameters, it is also obvious that he assumes that one of his constructors gets used.

The straight forward reasoning: assume your class has multiple fields, and the user-written constructor initializes all of them. What should the compiler-generated default constructor do about these fields?! And as one of the comments pointed out: leaving them at null or maybe 0 isn't a good idea.