The This keyword in Java

Source: Internet
Author: User

  1. The This keyword appears in the constructor method of the class, and represents the object that is created by using the constructor method.

  2. public class people {   /**    *  @author   Shepherd Queen Berg     *  @param  args    * date:2015-10-14     */    private int age;    private string  name;    people (String name) {         This.name = name;        this.printinfo ();   // can also omit This    }    public void printinfo () {         age = 21;   //can also be used here  this.age         system.out.println ( name +  "Age is: "  + age );     }    public static void main (String[] args)  {    &nBsp;  people p = new people ("Queen of Sheep");  //this is the object in the constructor method  p    } }

3. Using this in an instance method, an instance method can only be called by an object and cannot be invoked with a class name. When this is present in an instance method, this represents the current object that is calling the method.

It is also important to note that when the name of the instance member variable is the same as the name of the local variable, the "this." or "class name" precedes the member variable. Can not be omitted.

public class demo {    public int x = 100;     public static int y;    public void a () {         int x =10;        demo.y  = 20;        //The This keyword cannot be omitted when the instance member variable is the same as the name of the local variable          system.out.println ("x = "  +x +  "\t this.x  =  " + this.x");         this. B ();  //  the current object is called b   it can also be said that    object D called method A, and then called the method b  this keyword can also be omitted at this time          c ();     }    public void  b () {        system.out.println ("Hello , b method  ! "  );     }&NBSp;   public static void c () {         System.out.println ("hello , c method !");     }    public static void main (String[] args)  {             Demo d =  New demo ();         d.a ();     }}

Note: The This keyword cannot appear in a class method because the class method can be called directly through the class name, at which point there may not be any objects created.

-------a deeper understanding of the This keyword: several uses of the This keyword in Java

The This keyword 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.