If no constructor exists in the class you write, the compiler will automatically create a default constructor (constructor without parameters) for you ).
However, if you have defined a constructor (whether there are parameters or not), the compiler will not automatically create a default constructor for you.
There is a class:
[Java]
Class Dog {
Private String name;
Dog (String name ){}
}
Class Dog {
Private String name;
Dog (String name) {} www.2cto.com
}
This class already has a constructor with parameters, so the compiler will not automatically create a default constructor without parameters. If you write this:
[Java] view plaincopyprint? New Dog ();
New Dog ();
The compiler reports an error: no matching constructor is found. This is like, if you do not provide any constructor, the compiler will think: "You need a constructor. Let me create one for you ", but if you have written a constructor, the compiler will think: "Ah, you have written a constructor, so you know what you are doing; you deliberately omit the default constructor."
Author: tianjf0514