What is a Java constructor?

Source: Internet
Author: User

It is boring to initialize all its variables for every instance that creates a class. If an object completes all the initial work when it is created, it is simple and concise. Therefore, Java provides a special member function in the class called constructor ).

A constructor is a member function of the initial object when an object is created. It has the same name as its class. Once a constructor is defined, it is automatically called when an object is created. The constructor does not return a type, even the void type does not. This is because the type of the returned value of A Class constructor is the class itself. The task of the constructor is to initialize the internal state of an object. Therefore, after an instance is created using the new operator, a clear and available object is obtained immediately.

Constructor is a special method with the following features.
(1) The method name of the constructor must be the same as the class name.
(2) The constructor does not return a type and cannot be defined as void. The method type is not declared before the method name.
(3) The main function of constructor is to complete object initialization. It can pass the parameters when defining an object to the object's domain.
(4) The constructor cannot call the constructor, but must call the constructor.
(5) A class can define multiple constructor methods. If no constructor is defined when a class is defined, the compilation system automatically inserts a default constructor without parameters, this constructor does not execute any code.
(6) The constructor can be overloaded and distinguished by the number, type, or order of parameters.

Java constructor Execution Process

Class initialization:
(1) the storage space of the initialization object is zero or NULL;
(2) Call the parent class constructor;
(3) Call the initialization expressions of class member variables and instance member variables in order;

Package strong; public class const {private int A, B, C; Public void const () {system. out. println ("constructor"); A = 3; B = 5; this. C = a + B;} public void test () {system. out. println ("the value of C:" + C);} public static void main (string [] ARGs) {const c = new const (); C. test ();}}

Output result:

The value of C: 0

Unexpected? Well, let's take a look at the above descriptions to find out the reason;

Public void const (){
System. Out. println ("constructor ");
A = 3;
B = 5;
This. c = A + B;
}

In fact, it is not a constructor of this class. Because this class does not declare constructor, a constructor without any parameters is used by default;

Now we remove void; the structure output is:

Constructor
The value of C: 8

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.