Easy Tutorial
For Competitive Exams

Java Programming This Keyword

What is 'this'?

this is a keyword in Java. It can be used inside the Method or constructor of Class.

this works as a reference to the current Object whose Method or constructor is being invoked.

The this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.

How to use this keyword with field?

this keyword can be very useful in the handling of Variable Hiding.

Example

class Demo {
 int variable = 5;
 void method() {
 int variable = 40;
 System.out.println("Value of Instance variable :" + this.variable);
 System.out.println("Value of Local variable :" + variable);
 }
public static void main(String args[]) {
Demo obj = new Demo();
 obj.method();
 }
}
Try it Yourself

How to use this Keyword with Constructor?

“this” keyword can be used inside the constructor to call another overloaded constructor in the same Class.

This is called the Explicit Constructor Invocation.

This occurs if a Class has overloaded constructors.

Example

class Demo {
 
 Demo() {
 this("Java");
 System.out.println("Inside Constructor without parameter");
 }
 
 Demo(String str) {
 System.out.println("Inside Constructor with String parameter as " + str);
 }
 
 public static void main(String[] args) {
 Demo obj = new Demo();
 }
}
Try it Yourself

Note:

this keyword can only be the first statement in Constructor.

A constructor can have either this or super keyword but not both.

How to use this Keyword with Method?

this keyword can also be used inside Methods to call another Method from same Class.

Example

class Demo {
 void methodOne(){
 System.out.println("Inside Method ONE");
 }
 
 void methodTwo(){
 System.out.println("Inside Method TWO");
 this.methodOne();
 }
public static void main(String[] args) {
 Demo obj = new Demo();
 obj.methodTwo();
 }
}
Try it Yourself

How to use this keyword as Method parameter?

Example

class Demo  {
 int i;
 
 void method() {
 method1(this);
 }
 
 void method1(Demo t) {
 System.out.println(t.i);
 }
}
class Demomain {
 
 public static void main(String[] args) {
 Demo obj = new Demo();
 obj.i = 10;
 obj.method();
 }
 
}
 
Try it Yourself

Solve this!!

14588.What is the output of this program?
public class ThisDemo {
 
    int a;
    int b;
    
ThisDemo(int a, int b){
 
    this.a=a;
    this.b=b;
        
}
 public static void main(String[] args) {
        
    ThisDemo obj = new ThisDemo(10, 20);
        
     System.out.println(obj.a);
     System.out.println(obj.b);
   }
 }
10 20
10 10
20 20
Error
14589.What is the output of this program?
public class ThisDemo {
 
    int a;
    int b;
    
ThisDemo(int a, int b){
 
    this.a=a;
    this.b=b;
        
}
 
public static void main(String[] args) {
        
ThisDemo obj = new ThisDemo();
        
  System.out.println(obj.a);
  System.out.println(obj.b);
   }
 }
10 20
10 10
20 20
Error
Explanation:
Error:The constructor ThisDemo() is undefined.
Share with Friends