Use of this in Java

Source: Internet
Author: User

The This in Java is ubiquitous and uses a lot, and now there are a few things to organize:
This
1. When the global variable has the same name as the local variable, the global variable is used (this refers to this class object)
Example has a class a{
String name;
void SetName (String name) {
THIS.name = name;
}
}
2, the construction method calls each other, at this time this refers to the class name
Note This can only be placed in the first sentence of the construction method
such as Class b{
String name;
B () {
This ("name");//The construction method with the string parameter is automatically called
}

B (String name) {
THIS.name = name;
}

}


1. This refers to the current object itself.
This reference should be added when you want to explicitly indicate a variable or function that uses the object's own in a class. As in the following example:

public class Hello {

String s = "Hello";

Public Hello (String s) {
System.out.println ("s =" + s);
System.out.println ("1, THIS.S =" + THIS.S);
THIS.S = s;
System.out.println ("2, THIS.S =" + THIS.S);
}

public static void Main (string[] args) {
Hello x=new Hello ("helloworld!");
}
}



Operation Result:

s = helloworld!
1-THIS.S = Hello
2-THIS.S = helloworld!


In this example, in the constructor hello, the parameter s has the same name as the variable s of class Hello, and if you operate directly on s then the parameter S is manipulated. To operate on the member variable s of class Hello, you should use this to refer to. The first line of the running result is to print directly to the parameter s passed in the constructor, and the second line is to print the member variable s, and the third line is to pass the parameter S value passed to the member variable s before printing, so the result is helloworld!

2. Pass this as a parameter
You can also use this when you want to pass yourself as an argument to another object. Such as:

public class A {
Public A () {
New B (this). Print ();
}

public void print () {
System.out.println ("Hello from a!");
}
}

public class B {
A;
Public B (a a) {
THIS.A = A;
}

public void print () {
A.print ();
System.out.println ("Hello from b!");
}
}

Operation Result:
Hello from A!
Hello from B!






In this example, object A's constructor, with new B (this), passes the object a itself as a parameter to the constructor of object B.


3. Note the This in the anonymous class and in the inner class.
Sometimes, we use some internal classes and anonymous classes, such as event handling. When this is used in an anonymous class, this refers to the anonymous class or the inner class itself. If we want to use the methods and variables of the outer class, we should add the class name of the outer class. As the following example:

public class A {
int i = 1;

Public A () {
Thread thread = new Thread () {
public void Run () {
for (;;) {
A.this.run ();
try {
Sleep (1000);
} catch (Interruptedexception IE) {}

}
}
}; notice here there;
Thread.Start ();
}

public void Run () {
System.out.println ("i =" + i);
i++;
}

public static void Main (string[] args) throws Exception {
New A ();
}
}








In the above example, thread is an anonymous class object, and in its definition, it uses the run function of the outer class. This is because the function has the same name and is not called directly. There are two ways to change the external run function to a name, but this approach is undesirable for a development-to-midway application. Then you can use the method in this example with the class name of the external class plus this to illustrate the way to invoke the external class of the run.


4. In the constructor, this can be used to call other constructors in the same class, such as

public class flower{
Flower (int petals) {}
Flower (String ss) {}
Flower (int petals, Sting ss) {
petals++; A statement that calls another constructor must be at the beginning of the
This (petals);
This (ss); generates an error because only one constructor can be called in a constructor
}
}


It is worth noting that:
1: In the construction call to another constructor, the call action must be placed at the starting position.
2: The constructor cannot be called within any function other than the constructor.
3: Only one constructor can be called within a constructor.

Use of this in Java

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.