Java Learning Notes (vii)

Source: Internet
Author: User

Construction method 1.1 Construction Method Introduction First of all to understand the difference between the construction method and the general method:

The construction method executes when the object is created and executes only once.

The general method is called by the object after it is created, and can be called multiple times.

Characteristics of the construction method:

The construction method does not return a value type. You do not need to write a return value. Because it is for building objects, the object is created, and the method executes the end.

The construction method name must be consistent with the type.

The constructor method does not have a specific return value.

Format of the construction method:
Modifier constructor Method Name (parameter list) {}
The code embodiment of the construction method
class Person {    //  Person's member property age and name    privateint age ;     Private String name;     // constructor method for person, with parameter list    Person (int  A, String nm) {        //  accepts the value passed in when the object was created, assigns the value to the member property, age        = A;         = nm;    }}
1.2 Constructing method calls and memory plots

The constructor method is specifically used to create the object, that is, to invoke the constructor method when the new object is created. Now let's look at how to invoke the constructor method.

classPerson {//Person 's member property age and name    Private intAge ; PrivateString name; //constructor method for person, with parameter listPerson (intA, String nm) {        //accepts values passed in when an object is created, assigns values to member propertiesAge =A; Name=nm; }     Public voidspeak () {System.out.println ("Name=" + name + ", age=" +Age ); }}classPersondemo { Public Static voidMain (string[] args) {//Create a Person object and define the age and name of the objectperson P2 =NewPerson (23, "Zhang San");    P2.speak (); }}

When the object is created, the constructor method corresponding to the argument list is called. Process can

Schematic Description:

1. The main method is pressed into the stack first, and the new person (23, "Zhang San") in the main method is executed.

2. Allocate an area in the heap memory to hold the created Person object, and this memory area will have its own memory address (0x88). The member variables are then initialized by default (name=null,age=0).

3. Execute the code in the constructor method (age = A; name = NM;), assign 23 of the variable A to age, assign the "Zhang San" of the variable NM to name, and after the execution of this code, the value of the member variable age and name has changed. The method stack is constructed after execution finishes, and the person object is created. Assigns the memory address of the person object 0x88 to P2.

1.3 Default constructor method

During the previous study, when describing things, the specified construction method was not displayed, and when compiling the Java file, the compiler automatically added the default constructor to the class file. If we show that the constructor is specified when describing the class, the compiler will no longer add a default constructor to the class file when compiling the Java source file.

class Person   {    // If the specified constructor method is not displayed, compilation    automatically adds the default constructor at compile time //person() {}   // default construction method for null parameters }

Details of the construction method:

1, a class can have more than one construction method, more than one construction method exists in the form of overloading

2, the construction method can be modified by private, function: Other programs cannot create objects of this class.

 class   person { private  int   age;     private   String name;  //  Private parameterless construction method where the outside world cannot pass new person (); Statement to create this class object  private   person () {}  
   
    // 
     Multiple construction methods exist as overloaded  person (
    int  
     a) {age  =
     A; } person (String nm,  
    int  
     a) {name =
     nm;    Age  =
     A; }}
   

Java Learning Note (vii)

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.