Java this Keyword - The Coding Shala

Home >> Learn Java >> Java This Keyword

Java this Keyword

Java this keyword is a reference variable that refers to the current object.

The following are the ways to use Java this keyword:
  • Java this keyword can be used to refer to the current class instance variables. We can use current class instance variables using this keyword. The following example explains it: 
class Student{
 int id;
 String name;
 
 Student(int id, String name){
  this.id = id;  
  this.name = name;  //this.name represent current class variable
 }
}

class Main{
 public static void main(String[] args) {
  Student s1 = new Student(1, "Akshay");
 }
}

  • We can also use Java this keyword to invoke the current class constructor. The following example explains it: 
class Student{
 int id;
 String name;
 Student(){
  this(1, "Akshay"); //invoke current class constructor
 }
 
 Student(int id, String name){
  this.id = id;  
  this.name = name;  //this.name represent current class variable
 }
}

class Main{
 public static void main(String[] args) {
  Student s1 = new Student();
 }
}


  • Java this keyword can also be used to return the current class instance. The following example explains it: 
class Student{
 int id;
 String name;
 Student(){
  this(1, "Akshay"); //invoke current class constructor
 }
 
 Student(int id, String name){
  this.id = id;  
  this.name = name;  //this.name represent current class variable
 }
 
 Student Get() {
  return this;
 }
 
 void Display() {
  System.out.println("Student info below");
  System.out.println("Id of Student is: "+id);
  System.out.println("Name of Student is: "+name);
 }
}

class Main{
 public static void main(String[] args) {
  Student s1 = new Student();
  s1.Get().Display();
  //here s1.Get() will return current class instance
  //and then it will call Display Method
 }
}
Output: 

Student info below
Id of Student is: 1
Name of Student is: Akshay



  • Using Java this keyword we can also invoke the current class method just like a constructor. The following example explains it: 

class Student{
 int id;
 String name;
 Student(){
  this(1, "Akshay"); //invoke current class constructor
 }
 
 Student(int id, String name){
  this.id = id;  
  this.name = name;  //this.name represent current class variable
  this.Display();  //will invoke the class method Display
 }
 
 void Display() {
  System.out.println("Student info below");
  System.out.println("Id of Student is: "+id);
  System.out.println("Name of Student is: "+name);
 }
}

class Main{
 public static void main(String[] args) {
  Student s1 = new Student();
 }
}
Output: 

Student info below
Id of Student is: 1
Name of Student is: Akshay


  • We can also pass Java this keyword as a method parameter. We can also pass the Java this keyword in the constructor also. The following example explains it: 
class Student{
 int id;
 String name;
 Student(){
  this(1, "Akshay"); //invoke current class constructor
 }
 
 Student(int id, String name){
  this.id = id;  
  this.name = name;  //this.name represent current class variable
  this.Display(this);  //passing this as a parameter
 }
 
 void Display(Student s) {
  System.out.println("Student info below");
  System.out.println("Id of Student is: "+s.id);
  System.out.println("Name of Student is: "+s.name);
 }
}

class Main{
 public static void main(String[] args) {
  Student s1 = new Student();
 }
}
Output: 

Student info below
Id of Student is: 1
Name of Student is: Akshay



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Introduction to Kotlin Programming Language for Backend Development - The Coding Shala