This question already has an answer here:
- Java default constructor 11 answers
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?"
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.
来源:https://stackoverflow.com/questions/55965999/why-default-constructor-is-not-provided-by-compiler-when-class-contains-parametr