• August 26, 2014
  • Core Java
  • Comments Off on this keyword in java

this keyword in java

 this keyword:

this is a keyword in java which refers to the current instance of the class. Every constructor and all non-static methods in java have this as an implicitly parameter.

Note: When n numbers of parameters are passed in a method then from JRE point of view n+1 number of parameters are passed in the method. One additional parameter is this. If this keyword is used in constructor or method then this must be the first statement. 

Use of this keyword in java:

  1. 1. this can be used to differentiate between instance variable and local variable.
  2. 2. this can be used in constructor chaining. this() and super() must be the first statement.
  3. 3. this can be used to invoke current class method implicitly.
  4. 4. this can be used to return current instance of the class.
  5. 5. this can be used as a parameter in constructor call.
  6. 6. this can be used as a parameter in method call.

1. this can be used to differentiate between instance variable and local variable.

If local variable and instance variables are same than compiler will not be able to distinguish them.

Example: 

ThisExample1.java

/**
 * This program is used to show that if local variable and
 * instance variables are same than compiler will not be able
 * to distinguish them.
 * @author javawithease
 */
class Student{
      //instance variable.
      int rollNo;
      String name;              
 
      Student(String name, int rollNo){
           //local variable.
           name = name;
           rollNo = rollNo;
      }               
 
      public void displayDetails(){
             System.out.println("RollNo = " + rollNo);
             System.out.println("name = " + name);
      }
}
 
public class ThisExample1 {
       public static void main(String args[]){
              //creating Student class object.
              Student stu1 = new Student("jai", 6);
              //method call
              stu1.displayDetails();
      }
}

Output:

RollNo = 0
name = null

Download this example.

If local variable and instance variables are same than compiler will not be able to distinguish them. this can be used to differentiate between instance variable and local variable

Example:

ThisExample2.java

/**
 * This program is used to show that if local variable and
 * instance variables are same than compiler will not be able
 * to distinguish them. This problem is resolved using this keyword.
 * @author javawithease
 */
class Student{
      //instance variable.
      int rollNo;
      String name;               
 
      Student(String name, int rollNo){
             //local variable.
             this.name = name;
             this.rollNo = rollNo;
      }               
 
      public void displayDetails(){
             System.out.println("RollNo = " + rollNo);
             System.out.println("name = " + name);
      }
}
 
public class ThisExample2 {
       public static void main(String args[]){
              //creating Student class object.
              Student stu1 = new Student("jai", 6);
              //method call
              stu1.displayDetails();
       }
}

Output:

RollNo = 6
name = jai

Download this example.

If local variable and instance variables are different than no need of this keyword.

Example:

ThisExample3.java

/**
 * This program is used to show that if local variable and
 * instance variables are different than no need for this keyword.
 * @author javawithease
 */
class Student{
      //instance variable.
      int rollNo;
      String name;               
 
      Student(String n, int r){
             //local variable.
             name = n;
             rollNo = r;
      }
 
      public void displayDetails(){
              System.out.println("RollNo = " + rollNo);
              System.out.println("name = " + name);
      }
}
 
public class ThisExample3 {
       public static void main(String args[]){
              //creating Student class object.
              Student stu1 = new Student("jai", 6);
 
              //method call
              stu1.displayDetails();
       }
}

Output:

RollNo = 6
name = jai

Download this example.

2. this can be used in constructor chaining.

Constructor chaining:

Constructor chaining is a process of calling one constructor from other constructor of a class on an object.

Example:

ThisExample7.java

/**
 * This program is used to show the use of this keyword
 * in constructor chaining.
 * @author javawithease
 */
class Display {
      int a, b;
 
      //default constructor.
      Display(){
          //call two parameter constructor using this.
          this(10, 20);
           System.out.println("Default Constructor called.");
     }             
 
     //one parameter constructor.
     Display(int num1){
             //call two parameter constructor using this.
             this(num1, 40);
             System.out.println("one parameter constructor called.");
     }              
 
     //two parameter constructor.
     Display(int num1, int num2){
            a = num1;
            b = num2;
            System.out.println("two parameter constructor called.");
     }              
 
     //method to display values.
     public void display(){
            System.out.println("a = " +a);
            System.out.println("b = " +b);
     }
}
 
public class ThisExample7 {
        public static void main(String args[]){
               //call default constructor.
               Display obj1 = new Display();
               obj1.display();
 
              //call one parameter constructor.
              Display obj2 = new Display(30);
              obj2.display();
 
              //call two parameter constructor.
              Display obj3 = new Display(50, 60);
              obj3.display();
       }
}

Output:

two parameter constructor called.
Default Constructor called.
a = 10
b = 20
two parameter constructor called.
one parameter constructor called.
a = 30
b = 40
two parameter constructor called.
a = 50
b = 60

Download this example.

3. this can be used to invoke current class method implicitly.

Method chaining:

Method chaining is a process of calling multiple methods on an object in a single statement.

Example:

ThisExample4.java

/**
 * This program is used to show that this keyword
 * can call current class method implicitly.
 * @author javawithease
 */
class Display{
      public void displayName(){
             System.out.println("jai");
 
             //call current class method using this
             this.displayRollNo();
 
             }
 
             public void displayRollNo(){
                    System.out.println("6");
 
                    //compiler will automatically add this
                    //keyword if not used.
                    displayClass();
                }
 
                public void displayClass(){
                       System.out.println("MCA");
                }
}
 
public class ThisExample4 {
       public static void main(String args[]){
              //create Display class object
              Display display = new Display();
 
             //method call
             display.displayName();
       }    
}

Output:

jai
6
MCA

Download this example.

4. this can be used to return current instance of the class.

Example:

ThisExample6.java

/**
 * This program is used to show that this keyword
 * returns the instance of current class.
 * @author javawithease
 */
class Display{
      public Display getDisplay(){
             //this return the current class object.
             return this;
      }
 
      public void display(){
             System.out.println("Hello javawithease.com");
      }
}
 
public class ThisExample6 {
       public static void main(String args[]){
              //create Display class object
              Display display = new Display();
 
              //method call, here getDisplay() returns the
              //object of current Display class.
              display.getDisplay().display();
       }  
}

Output:

Hello javawithease.com

Download this example.

5. this can be used as a parameter in constructor call.

Example:

ThisExample8.java

/**
 * This program is used to show that this keyword
 * can be passed as an argument in the constructor.
 * @author javawithease
 */
class Display{
      Display(Show obj){
             System.out.println("Show obj = " + obj);
      }
}
 
class Show{
      Show(){
           //pass show class object as an argument using this.
           Display obj = new Display(this);
      }
}
 
public class ThisExample8 {
       public static void main(String args[]){
               //create Show class object
               Show show = new Show();
       }     
}

Output:

Javawithease
com.javawithease.business.Display@dc8569

Download this example.

6. this can be used as a parameter in method call.

Example:

ThisExample5.java

/**
 * This program is used to show that this keyword
 * can be passed as an argument in the method.
 * @author javawithease
 */
class Display{
      public void displayName(){
             System.out.println("javawithease");
 
             //passing this keyword as an argument.
             displayObject(this);
     }
 
     public void displayObject(Display obj){
            //will print string representation of the object.
            System.out.println(obj);
     }
}
 
public class ThisExample5 {
        public static void main(String args[]){
                //create Display class object
                Display display = new Display();
 
                //method call
                display.displayName();
       }     
}

Output:

Show obj = com.javawithease.business.Show@1bab50a

Download this example.

Note: this is a final variable, so you can’t assign any value to this. this can’t be used in static method.

 
Next Topic: Instance initialize/ Anonymous block in java with example.
Previous Topic: Package class in java with example.

Related Topics:

Package in java with example.
Access modifier in java with example.
Static import in java with example.
Package class in java with example.
this in java with example.
Instance initialize/ Anonymous block in java with example.
super in java with example.
Static in java with example.
final in java with example.

 


Comments are closed.