Getting started in the Java language Tutorial (v): Constructing methods in the Java language

Source: Internet
Author: User
Tags constructor modifiers

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.