this keyword in Java

this keyword:

      "this" is a keyword that is a reference variable that is used to refer the current object.

Use of this keyword:

  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
Example program: where this keyword is necessary ?? Let's us see the simple example

class Example
  {
     int id;
     String name;
       Example(int id, String name)
         {
             id=id;
             name=name;
          }
         void display()
           {
              System.out.println(+id+" " +name);
           }
 public static void main(String args[])
   {
       Example obj=new Example(8027, "Klu");
       obj.display();
    }
 }

Output:
0 null

How to solve such problems ??

class Example
  {
     int id;
     String name;
       Example(int id, String name)
         {
             this.id=id;
             this.name=name;
          }
         void display()
           {
              System.out.println(+id+" " +name);
           }
 public static void main(String args[])
   {
       Example obj=new Example(8027, "Klu");
       obj.display();
    }
 }

Output:
8027 Klu

      Reason we're using this keyword in the same program is, parameter (formal arguments) and instance variables are same that is why we are using this keyword to distinguish between local variable and instance variable.

       Important Note: If the programmer used different named Instance variable and Local variable in that time there is no need to specify the this keyword.

Example:

class Example
  {
     int id;
     String name;
       Example(int id, String name)
         {
             id=id;
             name=name;
          }
         void display()
           {
              System.out.println(+id+" " +name);
           }
 public static void main(String args[])
   {
       Example obj=new Example(8027, "Klu");
       obj.display();
    }
 }

Output:
0 null

How to solve such problems ??

class Example
  {
     int id;
     String name;
       Example(int a, String b)
         {
             id=a;
             name=b;
          }
         void display()
           {
              System.out.println(+id+" " +name);
           }
 public static void main(String args[])
   {
       Example obj=new Example(8027, "Klu");
       obj.display();
    }
 }

Output:
8027 Klu

This program runs smoothly without any error.


this()

this() can be used to invoke the current class constructor.

class This_use

    int id;
    String name; 
    This_use() // constructor
       {
          System.out.println("default constructor is invoked");
        } 
     
    This_use(int id,String name)
    { 
    this ();   //it is used to invoked current class constructor. 
    this.id = id; 
    this.name = name; 
    } 
    void display()
    {
       System.out.println(id+" "+name);
    }    
 public static void main(String args[])
    { 
    This_use e1 = new This_use(8027,"Sathish");
    This_use e2 = new This_use(8034,"Yogesh");
    e1.display(); 
    e2.display(); 
   } 
}

Output:
default constructor is invoked
default constructor is invoked
8027 Sathish
8034 Yogesh

Another Example of this()

class Another_this_use

    int id; 
    String name; 
    String city; 
     
    Another_this_use(int id,String name)
    { 
    this.id = id; 
    this.name = name; 
    } 
    Another_this_use(int id,String name,String city)
   { 
    this(id,name);    //No need to initialize id and name 
    this.city=city; 
    } 
    void display()
    {
           System.out.println(id+" "+name+" "+city);
    } 
  public static void main(String args[])
    { 
    Another_this_use e1 = new Another_this_use(8000,"F-name"); 
    Another_this_use e2 = new Another_this_use(8001,"S-name","Another"); 
    e1.display(); 
    e2.display(); 
   } 
}

Output:
8000 F-name null
8001 S-name Another

      This example is used to this() constructor is used to reuse the constructor in the another constructor of the same class.

Important**: this() should be in the first line of the constructor. If it's replaced the compile time error may occurred.
                  






No comments:

Post a Comment