A detailed description of the three uses of this in Java

Source: Internet
Author: User
Tags variable scope

The This keyword must be placed in a non-static method

The This keyword represents itself, and the main uses in the program are the following:

Referencing member variables using the This keyword

Use the This keyword to refer to other construction methods inside of its own constructor

Use the This keyword to represent an object of your own class

Use the This keyword to refer to member methods

1 Referencing member variables

Inside a method or constructor of a class, you can use the format "this. Member variable name" to refer to the member variable name, sometimes omitted, and sometimes not omitted. First look at the following code:

/**

* Use this to refer to member variables

*/

public class Referencevariable {

private int A;

Public referencevariable (int a) {

THIS.A = A;

}

public int Geta () {

return A;

}

public void SetA (int a) {

THIS.A = A;

}

}

Within the constructor and Seta methods of the code, the member variables of the class are referenced with THIS.A. Because both the constructor method and the Seta method contain 2 variables named A, one is parameter a, and the other is member variable A. According to the scope of the variable scope of the Java language, the scope of the parameter A is the construction method or method inside, the scope of the member variable A is the inside of the class, so there is a conflict of variable A in the construction method and the Seta method, the Java language stipulates that when the variable scope overlaps, A variable with a small scope overrides a variable with a large scope. Therefore, within the constructor method and the Seta method, parameter a functions.

This requires access to the member variable A, which must be referenced using this. Of course, if the variable name does not overlap, this can be omitted.

However, in order to enhance the readability of the code, the name of the parameter and the name of the member variable are generally consistent, so the frequency of this use should be much within the code of the specification.

2 Reference Construction methods

Inside the constructor of a class, you can also use the This keyword to refer to other construction methods, which can reduce the duplication of code, but also can make all the construction method is uniform, so as to facilitate later code modification and maintenance, but also convenient code reading.

The following is a simple example:

/**

* Use the This keyword to reference the construction method

*/

public class Referenceconstructor {

int A;

Public Referenceconstructor () {

This (0);

}

Public referenceconstructor (int a) {

THIS.A = A;

}

}

Here in the constructor method without parameters, using this call to another constructor, where 0 is the value of the parameters passed according to need, when a class inside the construction method is relatively long, you can only write a constructor method of the internal function code, and then the other construction methods are called by the construction method to implement, This ensures that all constructs are unified, and that the code duplication is reduced.

In practice, it is important to note that when other construction methods are invoked inside the constructor using the This keyword, the code that is called can only appear in the first line of executable code inside the constructor method. In this way, a constructor method called using the This keyword inside a constructor method appears at most one time.

3 represents an object of its own

Inside a class, you can also use this to represent an object of your own class, or in other words, there is an implied member variable inside each class that is the type of the class, the name of the member variable is this, and the example code that actually uses this object that represents its own class is as follows:

/**

* Use this to represent the object of your class

*/

public class Referenceobject {

Referenceobject instance;

Public Referenceobject () {

instance = this;

}

public void Test () {

System.out.println (this);

}

}

Within the constructor method, the value of this object is assigned to instance, inside the test method, the contents of this object are output, and this represents the object of its type.

4 Referencing Member methods

Within a class, you can also use the "this. method name (parameter)" to refer to each other when calling between member methods, except that all such references can be omitted, so this is not covered in detail here.

A detailed description of the three uses of this in Java

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.