Java Learning Note 12 (Object oriented Five: Construction method, this re-explore)

Source: Internet
Author: User

In development, it is often necessary to define the property values of object pairs while creating objects,

For example, when a person object is created, it should have attributes such as age and name.

So how do you initialize a value for an object's properties while creating the object?

Here are the construction methods:

1. The construction method does not return a value type, there is no return value, because it is a build object, the object is created, the method executes the end

2. Constructor method names must be consistent with type

3. When does the construction method run? A: Automatically when you create an object, and run only once

Definition Example:

 Public class Person {    public person  () {        System.out.println ("I am a constructor of an empty parameter");}    }

Run:

 Public class Test {    publicstaticvoid  main (string[] args) {        new Person ();    }} // output: I am an empty parameter construction method

The construction method is the necessary content of each class, either written or not written.

The compiler (JAVAC) will detect at compile time whether there is a construction method, if there is, execute, if not, automatically create an empty argument empty content construction method

The assignment of the constructor method:

 public  class   person { private      String name;  private  int      age;  public  person (String name,int  Span style= "COLOR: #000000" > age) { this . Name = name;     this . Age = age; }}
 Public class Test {    publicstaticvoid  main (string[] args) {        new Person ("Zhang San");    }}

Construction Method Memory Analysis:

1.main method to run in a stack with only one line: Create object

2. In-heap open space is stored in the object with two parameters

3. Two private variables follow into heap memory and assign default values (null,0)

4. The object passes its own address to the This keyword

5. The object calls its own construction method, constructs the method to run in the stack, and the object passes two parameters into the

6. Construction method accesses the heap memory object in this way and assigns a value to the member variable

7. Construction method Run end, object set up, address passed to P

The constructor method can be overloaded

Example:

 public  class   person { private      String name;  private  int      age;  public   person () {}  public  person (String name, int   age)        { this . Name = name;     this . Age = age; }}
 Public class Test {    publicstaticvoid  main (string[] args) {        new Person ("Zhang San");         New Person ();    }}

This is called between the constructor methods:

 Public classPerson {PrivateString name; Private intAge ;  PublicPerson () {//this () is called by other constructor methods//This () must be written in the first line of the construction method         This("Zhang San", 20); }     PublicPerson (String name,intAge ) {         This. Name =name;  This. Age =Age ; }}
 Public class Test {    publicstaticvoid  main (string[] args) {        new Person ("Zhang San");         New Person ();    }}

Memory principle:

1.main method into the stack run, open space in the heap to create objects, member variables follow and assign default values

2. The object calls its own constructor method, calls the empty argument constructs, runs in the stack, the argument passes

3. Call the parameter construction method, the parameter construction method into the stack run

4. The assignment is completed by the end of the argument construction method, the Null parameter construction method ends, and the object creation is completed

5. Object address to P1 (p2)

Java Learning Note 12 (Object oriented Five: Construction method, this re-explore)

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.