Constructors in Java (constructor method)

Source: Internet
Author: User

Constructor, is a special method. It is used primarily to initialize an object when it is created, to assign an initial value to an object member variable, and to always use the new operator in the statement that creates the object. A particular class can have more than one constructor, which can be distinguished by its number of arguments or by the different types of arguments, which are overloads of the constructor.

One, the characteristics of the constructor:

    1. The main function of the constructor is to do the initialization of the object,(if there is no constructor in the written class, the compiler defaults to a constructor that has no parameters and the method body is empty). It is able to pass parameters that define an object to the domain of the object. This means that when an object is created, the object is initialized. If the constructor is not empty at this point, the code inside the constructor is executed when the object is created:
    2. The name of the constructor must be the same as the class name, including case;
    3. The constructor does not have a return value and cannot be decorated with void. If you accidentally add a return value type to the constructor, this will make the constructor a normal method, and at run time will produce an error that the constructor method cannot be found.
    4. A class can define multiple construction methods, and if a constructor is not defined when the class is defined, the compiled system automatically inserts a default constructor with no parameters, which does not execute any code.
    5. The constructor method can be overloaded, with the number of parameters, type, order.

Second, the function of the constructor

    1. First, the constructor is used to build and initialize the object
    2. Constructors can use the override of a function to assign an initial value to an object, simplifying the code, such as:

This code creates two person objects, which are assigned to the initial values. But if you create more objects, the code is very repetitive. You can overload the constructor method inside the person class to give them the initial value when you create the object. This saves you from repeating the code:

Here's a question that requires special attention: By default, a class has a default constructor, which has no content and no return value, and is generally omitted from writing. In this case, the compiler defaults to a constructor that is not a parameter and the method body is empty when compiling. However, If the constructor of the class is overridden, as in the previous example, the person class already has a constructor with parameters that have a method body, then the compiler will not give it a default with a constructor that has no parameters and the method body is empty. A constructor that can be understood as an argument is overwritten. This is known as no default constructor .

In the inheritance of the function, the subclass must call the constructor of the parent class. However, a subclass can inherit only the default constructor of the parent class , and if the parent class does not have a default constructor, the subclass cannot inherit the default constructor from the parent class. At this point the subclass must use Super to implement a call to a non-default constructor of the parent class.

When you create an object, you first call the parent class default constructor to initialize the object, and then call the subclass itself to define the constructor.

Constructors in Java (constructor method)

Related Article

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.