The basic concepts of Java classes, packages in the Java language, permission access modifiers, and data types have been learned from the previous 4 articles. Java is an object-oriented language, and the runtime is the process by which several objects interact with each other and send messages to each other. For starters, it's important to understand how to create objects with Java classes.
If you have a class named customer, you must call the construction method with the New keyword to create the object of the class. For example, the customer class would have the following 3 construction methods:
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;
}
Based on this example, the characteristics of the construction method are summarized:
1. The constructor method must have the same name as the class name, and the case must be identical.
2. The construction method can be decorated with four permission modifiers.
3. The constructor does not have a return value type, note that there is not even void.
4. The construction method of a class can have multiple, mainly by parameter difference.
5. The method body of the constructor can write any statement that conforms to the Java syntax, but the constructor method is used most of the time to initialize the data member.
Using the constructor method, you have the following syntax:
Customer Cust=new construction method;
For the construction method, there is another problem that needs special attention, which is the default construction method problem. If a class does not explicitly declare a constructor, then there is a default constructor, which has no arguments and the method body is empty, such as:
public Customer() {
}
However, as long as the class declares the constructor method, the parameterless construction method does not exist by default, and must be explicitly declared if it is to be used. With respect to the construction method, there are some details to be noted when inheriting, which will be introduced later posting.