Home  >  Article  >  Java  >  what is java constructor

what is java constructor

(*-*)浩
(*-*)浩Original
2019-11-09 11:31:0514129browse

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.

what is java constructor

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():

what is java constructor

The above is the detailed content of what is java constructor. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java enumerationNext article:what is java enumeration