Java-4.4 this

Source: Internet
Author: User

Java-4.4 this

In this chapter, we will discuss some of

1 this is used inside the class and points to the object reference.

 

package com.ray.ch01;public class Test {private Test getTest() {return this;}public static void main(String[] args) {}}

In the above Code, this is the object that returns the Test type.

 

You can also perform this operation:

 

package com.ray.ch01;public class Test {private void say() {System.out.println(method say);}private void testSay() {this.say();}public static void main(String[] args) {new Test().testSay();}}
Output:

 

Method say

The above code references the Test object and operates the say method.

 

In fact, it doesn't matter if you don't use this inside the class, but it increases readability. The following code shows us this:

 

package com.ray.ch01;public class Test {private void say() {System.out.println(method say);}private void testSay1() {this.say();}private void testSay2() {say();}public static void main(String[] args) {new Test().testSay1();new Test().testSay2();}}

Output:

 

Method say

Method say
 

The output result shows that internal methods can be called if this is not used.

 

2 this can better differentiate between parameters and attribute fields.

During encoding, the names of many parameters are the same as those of the attribute fields. Therefore, using this allows us to better distinguish the two.

For example:

 

package com.ray.ch01;public class Test {private int id;public Test(int id) {id = id;}public static void main(String[] args) {}}

The above code is very difficult to distinguish between the two IDs, So we add this here to make the Code better understand.

 

 

package com.ray.ch01;public class Test {private int id;public Test(int id) {this.id = id;}public static void main(String[] args) {}}

3 this can reference the previous constructor.

 

 

package com.ray.ch01;public class Test {private int id;private String name;public Test() {System.out.println(created Test);}public Test(int id) {this();this.id = id;System.out.println(id: + id);}public Test(int id, String name) {this(id);this.name = name;System.out.println(name: + name);}public static void main(String[] args) {new Test(1, jack);}}

Output:

 

Created Test
Id: 1
Name: jack

This makes the code more indirect.

Note:

(1) The constructor referenced by this () must be placed in the first sentence. Otherwise, an error will be reported.

(2) The constructor referenced by this () cannot use more than two at the same time.

(3) this can only reference common methods, while this () can only reference constructors.

 

4. this cannot reference the static method.

 

package com.ray.ch01;public class Test {private static void say() {System.out.println(method say);}public static void main(String[] args) {Test.say();}}

A static method is a class method, which can be used without the need to create an object, but this method cannot be referenced by this.

 

Summary: this chapter mainly discusses the four points of attention of this and the implementation of the Points of attention in encoding.

 

2. this

2.1 this is used inside the class and points to the object reference.

 

package com.ray.ch01;public class Test {private Test getTest() {return this;}public static void main(String[] args) {}}

In the above Code, this is the object that returns the Test type.

 

You can also perform this operation:

 

package com.ray.ch01;public class Test {private void say() {System.out.println(method say);}private void testSay() {this.say();}public static void main(String[] args) {new Test().testSay();}}
Output:

 

Method say

The above code references the Test object and operates the say method.

 

In fact, it doesn't matter if you don't use this inside the class, but it increases readability. The following code shows us this:

 

package com.ray.ch01;public class Test {private void say() {System.out.println(method say);}private void testSay1() {this.say();}private void testSay2() {say();}public static void main(String[] args) {new Test().testSay1();new Test().testSay2();}}

Output:

 

Method say

Method say
 

The output result shows that internal methods can be called if this is not used.

 

2.2 this can better differentiate between parameters and attribute fields.

During encoding, the names of many parameters are the same as those of the attribute fields. Therefore, using this allows us to better distinguish the two.

For example:

 

package com.ray.ch01;public class Test {private int id;public Test(int id) {id = id;}public static void main(String[] args) {}}

The above code is very difficult to distinguish between the two IDs, So we add this here to make the Code better understand.

 

 

package com.ray.ch01;public class Test {private int id;public Test(int id) {this.id = id;}public static void main(String[] args) {}}

2.3 this can reference the previous constructor.

 

 

package com.ray.ch01;public class Test {private int id;private String name;public Test() {System.out.println(created Test);}public Test(int id) {this();this.id = id;System.out.println(id: + id);}public Test(int id, String name) {this(id);this.name = name;System.out.println(name: + name);}public static void main(String[] args) {new Test(1, jack);}}

Output:

 

Created Test
Id: 1
Name: jack

This makes the code more indirect.

Note:

(1) The constructor referenced by this () must be placed in the first sentence. Otherwise, an error will be reported.

(2) The constructor referenced by this () cannot use more than two at the same time.

(3) this can only reference common methods, while this () can only reference constructors.

 

2.4 this cannot reference the static method

 

package com.ray.ch01;public class Test {private static void say() {System.out.println(method say);}public static void main(String[] args) {Test.say();}}

A static method is a class method, which can be used without the need to create an object, but this method cannot be referenced by this.
Summary: this chapter mainly discusses the four methods of this and their implementation in coding.

 

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.