Constructors in Java classes are basically used

Source: Internet
Author: User

1. Basic Concepts:

A constructor is a special method in a class that is called when an object is instantiated. The constructor's purpose is to initialize the object's member variables when the appropriate object is instantiated. When we want to add a constructor for a class, the South no constructor name must be the same as the name of the class, and the return value cannot be declared.

If we add a constructor to a class called student:

 

1 /**2 *student class3   */4 5  Public classstudent{6      PublicString Studentname;//Student Name7      Public intStudentage;//Student Age8      PublicString Studentsex;//Student Sex9      PublicStudent(){TenSystem.out.println ("within a parameterless constructor. "); OneStudentname = "Anonymous"; AStudentage = "20"; -Studentsex = "Male"; -     } the      PublicStudent(String studentname,int studentage,string studentsex) { -System.out.println ("within the parameter constructor with three parameters. "); -Studentname =Studentname; -Studentage =Studentage; +Studentsex =Studentsex; -     } +    //class remainder Definition section A}

When you add more than one constructor to a constructor, the overloaded rules of the method are applied, and each constructor must have a unique formal argument list (the red part of the code above is the constructor's formal argument list), which differs from the other constructors.

2. The role of the constructor:

When an object is instantiated with the New keyword, the JVM (the Java Virtual Machine) allocates memory to the object and initializes it. Therefore, the value of the member variable of the object is set to the initial value. Without a constructor, we have to initialize all member variables to ensure that the object's data is meaningful. In the case of object instantiation, the constructor gives us a few minutes to construct the object to ensure that the object's member variables are meaningful data.

3. The difference between a constructor and a method:

①. The name of the constructor must be the same as the class name (the casing must also be the same).

②. The constructor cannot declare the return value, nor can it return void.

4. Default constructor:

If we write a class, but there is no constructor written in this class. This constructor is qualified with the public access modifier, has no formal parameters, and does nothing. The default constructor still follows the rules of the constructor.

If we write one or more constructors for a class, then the system will not generate the default constructor in the build.

5. Constructor Invocation Method:

When an object instantiates an object using the keyword new, the constructor is called. One can have more than one constructor, in which case the song constructor is called depending on the actual arguments used by the new operator.

When a class has only one constructor, there is only one way to initialize the class object. If a class has more than one constructor, the new operator can be used for the beauty constructors in the class.

New Student ();

The purple in the above code is called by the constructor. The only time a constructor can be called is when the object is instantiated.

Constructors in Java classes are basically used

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.