This Keyword Example Java Program

Definition

The keyword this is a Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods. Since all instance methods are virtual in Java, this can never be null.

Syntax

this.Variable_name = Variable_name;

This Keyword Example Program

class ClassOne {  
    public void getClassName() {  
        System.out.println("Class : " + this.getClass());  
    }  
}  

class ClassTwo {  
  
    public void getClassName() {  
        System.out.println("Class : " + this.getClass());  
    }  
}  

public class ThisKeywordDemo {  
    public static void main(String args[]) {  
        ClassOne obj1 = new ClassOne();  
        ClassTwo obj2 = new ClassTwo();  
        obj1.getClassName();  
        obj2.getClassName();  
    }  
}  

Sample Output

Output is:
Class : classOne
Class : classTwo