What is the construction method in Java language
Today, I will talk to you about the construction method in Java language, which may not be understood by many people. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope you can gain something according to this article.
The constructor section of the Java language.
Java language is an object-oriented language, runtime, is a number of objects interact with each other, send messages to each other process. For beginners, the first step is to understand how to create objects with Java classes.
If you have a class named Customer, then to create an object of that class, you must call the constructor with the new keyword. For example, the Customer class has three constructors:
public Customer() { } public Customer(String custname, String pwd) { this.custname = custname; this.pwd = pwd; } public Customer(String custname, String pwd, Integer age) { this.custname = custname; this.pwd = pwd; this.age = age; }
According to this example, summarize the characteristics of the construction method:
1. Constructor method names must have the same name as class names and must be exactly the same case.
2. Construction methods can be decorated with four permission modifiers.
3. Constructor methods have no return value type, note that void does not exist either.
4. A class construction method, there can be more than one, mainly by parameter distinction.
5. Constructors can write any statement that conforms to Java syntax, but constructors are mostly used to initialize data members.
Using construction methods, there is the following syntax:
Customer cust=new constructor;
Another problem with constructors in Java that needs special attention is the default constructor problem. If a class does not explicitly declare a constructor, then there is a default constructor that has no parameters and an empty method body, such as:
public Customer() { }
However, as long as the class explicitly declares a constructor, this parameterless constructor does not exist by default, and must be explicitly declared if it is needed.
After reading the above, do you have any further understanding of how construction methods are in the Java language? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.