Java08-Construction Method

Source: Internet
Author: User

Java08-Construction Method

First, the construction method

1, what is the construction method?

A constructor method (class method) is a special member method that is compatible with the method name and class name.

2, the role of the construction method?

When you create an object with the New keyword, the Java system automatically calls the construction method to initialize the new object after the new object has opened up a space in the heap memory.

3. Object initialization Process

Ii. definition and invocation of the construction method

1. Definition:

Class Name {

[Access rights] class name (parameter table) {

Statement body;

Construction method does not return a value

}

}

2. Call:

Class Name Object name = new constructor method name (argument table);

3, stack memory, heap memory

person P1 = new person ();

There are two objects in the preceding statement: reference objects and Memory objects

The specific storage methods are as follows:

Note: Stores opened by the new operator are in heap memory

Third, construction method: Initialize the object

1, the method name must be the same as the class name;

2, when defining the class, if there is no definition of the construction method, Java will automatically generate a non-parametric construction method, if the construction method is defined, will be automatically generated enough to cover the creation method.

3, the construction method has no return value, and the ordinary member method must have a return value type;

4, the construction method can only be called with the new operator, cannot be called with the object name;

Iv. Overloading of construction methods

1, method Overload: The method name is the same, the parameter list is different, the effect of the implementation is different;

2, the construction method of overloading: to the construction method to pass different parameters, you can achieve different forms of initialization;

3, the difference between the construction method and the common method: The construction method is a special method of the class, its particularity is as follows:

(1) can be overloaded;

(2) The method name is the same as the class name;

(3) cannot have return value, do not use return statement;

(4) cannot be explicitly called directly by the programmer (that is, the object name cannot be passed. Method name (); ), which can be called only through the new operator;

(5) Default empty construction method: In the Java language, each class has at least one construction method, if the exhausted definition does not explicitly define any construction method, the system will automatically provide a default construction method, if one or more construction methods are defined, the system will no longer default.

4. What is the difference between a construction method and an ordinary member method?

(1) The constructor method name is the same as the class name, and the member method is customized;

(2) The construction method has no return value, that is, there is no method type;

(3) The construction method is called by the system to instantiate the object;

Java08-Construction Method

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.