Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

this keyword in java

Posted By: Blanca Boyd     Category: Java     Views: 17555

This article explains about this keyword with different examples in java.

this keyword refers to the object that is currently executing. It is useful for a method to reference instance variables relative to the this keyword.

Syntax of using this keyword

this.varName

varName is a name of an instance variable.

Another use of the this keyword is it allows one constructor to explicitly invoke another constructor in the same class.

Syntax to call another constructor of same class using this keyword  

this(args);

Examples of this keyword

Example 1 : Program that creates class Square and assigns values to variables using constructor and this keyword

class Square
{
       int height;
       int width;

         Square(int height, int width)
         {
               // Note that constructor variable and instance variable names are same
               
                this.height = height
                this.width = width;
          }
}

class ImplSquare
{
     public static void main(String args[])
     {
             Square sObj1 = new Square(2,3);             

             System.out.println("Variable values of object : ");
             System.out.println("Object height =  " + sObj1.height);
             System.out.println("Object width = " + sObj1.width);
      }
}


Output

Variable values of object : 
Object height =  0
Object width =  0

As in above example you can note that constructor variable and instance variable names are same. But using this keyword compiler can differentiates constructor variables and instance variables.

Example 2 : Program that illustrates how this keyword can be used by one constructor to explicitly invoke another constructor in the same class

class Square
{
       int height;
       int width;

       Square()
       {
               this(0,0);
        }
        Square(int side)
        {
               this(side, side);
         }
         
         Square(int height, int width)
         {
                this.height = height;
                this.width = width;
          }
}

class ImplSquare
{
     public static void main(String args[])
     {
             Square sObj = new Square(4,6);
             
             System.out.println("Variable values of object : ");
             System.out.println("Object height =  " + sObj.height);
             System.out.println("Object width = " + sObj.width);
      }
}


Output

Variable values of object : 
Object height =  4
Object width =  6
  
Share: 


Didn't find what you were looking for? Find more on this keyword in java Or get search suggestion and latest updates.

Blanca Boyd
Blanca Boyd author of this keyword in java is from Chicago, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!