Java construction method: is a method defined in the Java class to initialize an object. Use the new construction method to create a new object and assign values to instances in the object.
Grammar rules:
1. The method name must be the same as the class name (Recommended learning: java course )
2. There is no return value type, and it cannot be modified with void (methods with any return value type are not constructors)
3. Parameters can be specified or not. ; Divided into parameterized constructor and parameterless constructor
Features of the constructor:
1. When no constructor is specified, the system will automatically add the parameterless constructor construction method.
2. Construction methods can be overloaded: multiple methods with the same method name but different parameters will automatically select the corresponding method based on different parameters when called.
3. The construction method is not inherited
4. When we manually specify the construction method, whether it has parameters or no parameters, the system will not add any more Constructor without parameters.
For example, we create a parameterized constructor Test() and a parameterless constructor Test():
The above is the detailed content of what is java constructor. For more information, please follow other related articles on the PHP Chinese website!