Constructors and overloads in Java

Source: Internet
Author: User

I. Constructors in Java

A constructor is a member method that initializes an object when it is created with exactly the same name as the class in which it resides. Constructors can have only entry parameters, no return type, because the class itself is the return class of the constructor method of a class. When the constructor is defined, it is automatically called when the object is created, allocating memory space and initialization for the newly created object. In Java, constructors can also be overloaded, and when an object is created, the JVM automatically matches the form-conforming construction method according to the current invocation form of the method in the definition of the class, and executes the construction method after the match succeeds.

eg

  Class dog{ private  int   age;  private   String name;  //  no parameter constructs  public   Dog () {}  //  Span style= "color: #0000ff;"    >public  Dog (int   age,string name) {    this . Age=age;   this . Name=name; }}

two. Default constructs in Java

If you omit the definition of a constructed method, Java automatically calls the default constructor method. However, if a construction method is defined, the default construction method is no longer provided by the system. The default constructor method does not have any parameters, and no action is taken. In fact, the function of the default constructor is to call the constructor with no arguments in the parent class of this class, and if such a constructor does not exist in the parent class, the compilation generates an error message. Object is the root of all classes in Java, defines its immediate subclasses, can omit the extends clause, and the compiler will automatically include it.

three. Method Overloading

The method names are the same, and the parameter items are different. Then one method is considered an overloaded method of another method.

Note: overloading is only relevant for parameters, regardless of the return type. The method name is the same as the parameter and the return type is not the same, and cannot be said to be overloaded.

public void Say (int.) {}

public int Say (int age,string name) {}

public string Say (String name,string age) {}

Four. Constructor method overloading

Construction method overloading is a typical special case of method overloading. The parameter list is different.

You can express the various initialization behaviors of an object by overloading the construction method. This means that when you create an object from the new statement, you can implement different initialization behaviors for different objects under different conditions.

public this. name=name;} Public Text (String name,string sex) {           this. name=name;             this. sex=sex;}}

Constructors and overloads in Java

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.