Xiao Qiao Note: java keyword this, Xiao Qiao java keyword this

Source: Internet
Author: User

Xiao Qiao Note: java keyword this, Xiao Qiao java keyword this

After an object is created in java, the java Virtual Machine assigns a pointer to the object, called this. This is generated with the instantiated object, so this is only used in non-static methods. There are four main applications:

1. Call the member variables of the current class. Such as: this. name; // access the member variable name in this class

2. Call the member methods of the current class. For example, this. fun (params...); // you can access the member method fun of params... in this class.

3. It indicates the reference of the current class object. For example, return this; // return the value of the current Class Object

4. Call other constructor of this class in the constructor of the current class. For example, this (params...); // call the constructor of the parameter params... in this class.

The following code is provided:

1.

 

public class Student {   String name;  private void SetName(String name){    this.name=name;  }}

 

As in the above Code, if a local variable has the same name as a member variable, this is displayed. This keyword references the member variable. At a glance, the parameter name is assigned to the member variable name.

2

public class Student {    private void out1(){     System.out.print("123");  }   private void out2(){      this.out1();  }  }

As in the code above, the out2 () method uses the this keyword to call the member method out1 (); note that, this keyword is used to access all member variables and methods that include private modification. You can also use this to access static members of this class. However, since static members can be directly accessed through the class name, there is a warning if this is used to access static members.

3.

public class Student {    private String name;    public Student setName(String name){     this.name=name;     return this;    }}  

As in the above Code, after the setName method is called and the form parameter is assigned to the member variable, return this indicates that the Student object reference is returned. Note that return this returns a reference to the object that calls the method of this. Do not confuse.

4.

Public class Student {private String name; private char sex; public Student () {this ('male'); this. name = "";} public Student (char sex) {this. sex = sex ;}}

For example, in the above Code, this ('male') is the second constructor called in the first constructor, that is, the one with no parameters called with parameters. It must be noted that it can only be placed in the first row of the constructor body, and does not support recursive calling, that is, it cannot call itself indirectly or directly.

Jojo Note: If this class has a derived class, you need to note the function of the keyword "this" in the class:

1. When calling the member method, if the method is overwritten by the quilt class, the member method of the subclass is called. If the method is not overwritten, the member method of the parent class is called.

2. this in return this always represents the reference of the subclass object.

3. When the member variables and constructor methods are called, they belong to the parent class and remain unchanged.

 

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.