Java -- this keyword summary, javathis keyword

Source: Internet
Author: User

Java -- this keyword summary, javathis keyword

I. Use of this keyword

1. When the same type of objects a and B call the same method (), to distinguish whether the method is called by a or B

In the following code ,:

class Banana { void method(int i) { /* ... */ } }Banana a = new Banana(), b = new Banana();a.method(1);b.method(2);

Internal compilation process:

1 Banana.method(a,1);2 Banana.method(b,2);

The first parameter in the parameter list of a method is the handle (reference) of the object that calls the method. During the compilation period, this is used to distinguish the call of the method.

Similar to the keyword self in the Python class,

2. this keyword (note that it can only be used inside the method) can generate a corresponding handle for the object that has called its method. Can Treat others

Treat this handle like an object handle.

If this keyword can be omitted when a method is called from this class, the Code is as follows:

1 class Apricot {2 void pick() { /* ... */ }3 void pit() { pick(); /* ... */ }4 }

This keyword can only be used for special classes -- the current object's handle must be used explicitly. For example, if you want to return the handle to the current

Is often used in return statements. The Code is as follows:

 1 public class Leaf { 2     private int i = 0; 3     Leaf increment() { 4         i++; 5         return this; 6     } 7         void print() { 8         System.out.println("i = " + i); 9     }10     public static void main(String[] args) {11         Leaf x = new Leaf();12         x.increment().increment().increment().print();13     }14 }        

Increment () returns the handle of the current object through the this keyword, so you can conveniently perform multiple operations on the same object

3. this keyword is called in the constructor.

3.1 although this can be used to call the build function, two cannot be called in the same constructor.

3.2 The constructor is the first to load during the class loading process. Otherwise, an error message is returned for the Compilation Program.

3.3 Use this to reference member data. This application is often seen in Java code to avoid confusion between member variables and method parameters.

3.4 The Compiler does not allow calls of a constructor from any method except a builder

The Code is as follows:

 1 public class Flower { 2     private int petalCount = 0; 3     private String s = new String("null"); 4  5     Flower(int petals) { 6         petalCount = petals; 7         System.out.println("Constructor w/ int arg only petalCount="+ petalCount); 8     } 9     Flower(String ss) {10         System.out.println("Constructor w/ String arg only, s=" + ss);11         s = ss;12     }13 14     Flower(String s, int petals) {15         this(petals);16         // ! this(s); // Can't call two!17         this.s = s; // Another use of "this"18         System.out.println("String & int args");19     }20 21     Flower() {22         this("hi", 47);23         System.out.println("default constructor (no args)");24     }25 26     void print() {27         // ! this(11); // Not inside non-constructor!28         System.out.println("petalCount = " + petalCount + " s = " + s);29     }30 31     public static void main(String[] args) {32         Flower x = new Flower();33         x.print();34     }35 }

 

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.