Home >Java >Javagetting Started >The difference between constructor methods and ordinary methods in java methods

The difference between constructor methods and ordinary methods in java methods

王林
王林Original
2019-11-28 10:25:512530browse

The difference between constructor methods and ordinary methods in java methods

Everyone must have seen ordinary methods. Methods modified with modifiers can have return values, and some have no return values.

What about the construction method? For example, you will understand quickly. When a class instantiates an object, the method used is the construction method. We can see that the construction method in a class is not introduced from other classes, but It's your own method. In other words, the constructor method is the method called when a class constructs an object, and is mainly used to instantiate the object.

Recommended java related video tutorials: java online tutorial

Let’s talk about the difference between the construction method and the ordinary method:

1. Construction method The name must be exactly the same as the class name in which it is defined. There is no return type, not even void.

2. There must be a constructor in the class. If not, the system will automatically add a parameterless constructor. Interfaces do not allow instantiation, so there are no constructors in interfaces.

3. Cannot be modified by static, final, synchronized, abstract and native .

4. The constructor is automatically executed when initializing the object, and generally cannot be called explicitly directly. When there are multiple constructors in the same class, the java compilation system will automatically follow the parameters in the last parentheses during initialization. Numbers and parameter types are automatically matched one-to-one. Complete the constructor call.

5. There are two types of construction methods: construction methods without parameters and construction methods with parameters

6. Construction methods can be overloaded. A constructor without parameters is called a default constructor. Like a general method, a constructor can perform any activity, but it is often designed to perform various initialization activities, such as initializing the properties of an object.

Recommended java related articles and tutorials: Introduction to java programming

The above is the detailed content of The difference between constructor methods and ordinary methods in java methods. 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