In this tutorial, we will learn about Usage of this in Java programming language
this is a keyword in Java that can be used to refer to the method or constructor of a class. this statements work as the reference to the current object which is an instance of the current class. The this keyword can be used to refer to any member of the current object from within an instance method or a constructor.
Uses of this keyword:
this keyword helps to avoid name conflicts
Usage of this in Java programming language
this.data_member of current class
class student{ String name; //instance variable 1 int id; //instance variable 2 student(String name,int id){// constructor with parameter(local variable) this.id=id; //assign local variable to global variable1 this.name=name; //assign local variable to global variable2 } }
In the Java programming language, never declare the variable with the same name inside a scope or block(class, method, constructor). However, instance or global variable and parameters ( passed local variable)may be defined with the same name.
class class_name{ int var_1; //instance variable named as var_1 void method_1(var_1){ // local variable named as var_1 //instance and local variable names are both the same. this.var_1=var_1 //use this keyword } }
This used to avoid the conflict in access to the variable due to the similarity in the same name.
Program 1
class Usage_this{ int stu_id; //instance variable for stu_id String stu_name; //instance variable stu_name Usage_this(int stu_id,String stu_name){//local variable for stu_id, stu_name //constructor for above class //parameters and instance variables both are same stu_id=stu_id; stu_name=stu_name; //assign local variable to global variable } void display() { System.out.println(stu_id +" "+stu_name); } public static void main(String args[]){ Usage_this obj=new Usage_this(32,"Jhon");//object for class obj.display(); } }
When the above code executes, it produces the following results
0 null
0 – default value for integer
null – default value for String
In the above program, Java compiler is confused due to name ambiguity. Therefore, the expected output is not present.
Because you might have expected 32, Jhon as an input
How to solve this problem? this keyword is used to solve this problem
class Usage_this{ int stu_id; //instance variable String stu_name; //instance variable Usage_this(int stu_id,String stu_name){ //constructor for above class //parameters same as instance variable this.stu_id=stu_id; this.stu_name=stu_name; //assign local variable to global variable //use this keyword } void display() { System.out.println(stu_id +" "+stu_name); } public static void main(String args[]){ Usage_this obj=new Usage_this(32,"Jhon");//object for class obj.display(); } }
When the above code is executed, it produces the following results
32 Jhon
//solve this problem using this keyword
this can be used to refer to the present class instance variable.
Program 1
class stu_det{ //start of class int age=41;//instance variable - integer String name="Kathir"; //instance variable - string public static void main(String args[]){//java main method stu_det obj=new stu_det(); //java oject obj.detail_one(12,"Khan"); //called metod_1 with argument obj.detail_two(13,"saman"); //called metod_2 with argument } void detail_one(int age,String name){//method_1 with parameter System.out.println("Student name is :"+name); System.out.println("Student age is :"+age); //print statement inside metod_1 } void detail_two(int age,String name){//method_2 with parameter System.out.println("Student name is :"+name); System.out.println("Student age is :"+age); //print statement inside metod_2 } }//end of class
In this case, we don’t need to use this keyword.
In this program, we can see the instance variable and its value hiding behind local variable.
Instance variable and local variable both are referred to by the same name. When the method is called, only the local (method) variable is displayed. global (instance) variable does not display.
When the above code is executed, it produces the following results
Student name is :Khan Student age is :12 Student name is :saman Student age is :13
Program 2
How to refer the instance(global) variable:
We can use this keyword to refer instance variable.
let’s understand this program
class stu_data{ int age=41; String name="Kathir"; public static void main(String args[]){ stu_data obj=new stu_data(); obj.detail_one(12,"Khan"); obj.detail_two(13,"saman"); obj.detail_three(); } void detail_one(int age,String name){ System.out.println("Student name is :"+name); System.out.println("Student age is :"+age); System.out.println("................."); } void detail_two(int age,String name){ System.out.println("Student name is :"+name); System.out.println("Student age is :"+age); System.out.println("................."); } void detail_three(){//method 3 using this keyword System.out.println("Student name is :"+this.name); System.out.println("Student age is :"+this.age); } }
This program is similar to program 1 but we have added a method named void detail_three with this keyword to refer to the global variable.
When the above code is executed, it produces the following results
Student name is :Khan Student age is :12 ................... Student name is :saman Student age is :13 ................... Student name is :kathir Student age is :41
In this program, we use this keyword to refer to the instance variable in method void detail_three().
Program 3
class Employee{ int emp_No; String emp_Name; double salary; Employee(int emp_No, String emp_Name,double salary){ emp_No=emp_No; emp_Name=emp_Name; salary=salary; } void display() { System.out.println("employee no is :"+emp_No); System.out.println("employee name is :"+emp_Name); System.out.println("employee salary is :"+salary+"\n"); } } class Emp_details{ public static void main(String args[]){ Employee e1=new Employee(12,"Jhon",23456.78); Employee e2=new Employee(22,"miccal",34567.89); e1.display(); e2.display(); } }
When the above code is executed, it produces the following results
employee no is :0 employee name is :null employee salary is :0.0 employee no is :0 employee name is :null employee salary is :0.0
Key to the above output:
0 is a default value of the integer
null is a default value of String
0.0 is a default value of double
In the above program, local variable(formal argument) and global(instance) variables are referred to by the same name.
We can use this keyword to distinguish between the local variable and the global variable.
Program 4
class Employee{ int emp_No; String emp_Name; double salary; Employee(int emp_No, String emp_Name,double salary){ this.emp_No=emp_No; this.emp_Name=emp_Name; this.salary=salary; } void display() { System.out.println("employee no is :"+emp_No); System.out.println("employee name is :"+emp_Name); System.out.println("employee salary is :"+salary+"\n"); } } class Emp_details{ public static void main(String args[]){ Employee e1=new Employee(12,"Jhon",23456.78); Employee e2=new Employee(22,"miccal",34567.89); e1.display(); e2.display(); } }
When the above code is executed, it produces the following results
employee no is :12 employee name is :Jhon employee salary is :23456.78 employee no is :22 employee name is :miccal employee salary is :34567.89
At the above program, this keyword is used to distinguish global variable from the local variable. Here is the expected output:
When the local variable and global variable are different, there is no need to use this keyword.
Here is an Example of the program when the variables are different:
In the above program, instance variables and local variables both are different. So, there is no need to use this keyword because java compiler automatically appends the this keyword.
Like this
class My_class{ int var_1 //instance variable } My_class (int var_2) //local variable or parameter { //constructor var_1=var_2;//no need use this keyword } //automatically append this keyword by compiler
it is equivalent to
class My_class{ int var_1 //instance variable } My_class (int var_1) //local variable or parameter { //constructor this.var_1=var_1; }
Program 4
class Employee{ int emp_No; String emp_Name; double salary; Employee(int emp_No1, String emp_Name1,double salary1){ emp_No=emp_No1; emp_Name=emp_Name1; salary=salary1; } void display() { System.out.println("employee no is :"+emp_No); System.out.println("employee name is :"+emp_Name); System.out.println("employee salary is :"+salary+"\n"); } } class Emp_details{ public static void main(String args[]){ Employee e1=new Employee(12,"Jhon",23456.78); Employee e2=new Employee(22,"miccal",34567.89); e1.display(); e2.display(); } }
When the above code is executed, it produces the following results
employee no is :12 employee name is :Jhon employee salary is :23456.78 employee no is :22 employee name is :miccal employee salary is :34567.89
this() can be used to invoke the current class method.
Program 1
//this example describe usage of this in method class This_method{ void this_One(){//method_1 System.out.println("this is method one"); } void this_Two(){//method_2 System.out.println("this is method Two"); this.this_One();//this refer to method_1 } public static void main(String args[]){ This_method obj=new This_method();//object for class obj.this_Two();//call the metod_2 } }
In this program, this keyword is used to refer to the current class method(refer method this_one inside this_two methods)
When the above code is executed, it produces the following results
This is method one This is method two
this() can be used to invoke the current class constructor
Program 1
//this example describe usage of this in constructor class This_cons{ This_cons(){//constructor_1 this("This_cons");//this refer to constructor System.out.println("this is a default constructor"); } This_cons(String name){//constructor_2 System.out.println("this is parameterized constructor"); } public static void main(String args[]){ This_cons obj=new This_cons();//object for class } }
When the above code is executed, it produces the following results
this is parameterized constructor this is a default constructor
this keyword is used to call parameterized constructor through default constructor
Program 1
class vehicle{ vehicle(){ //default constructor this(10); System.out.println("Motor car"); } vehicle(int a){ //parameterized constructor System.out.println(a); } } class Test_this{ public static void main(String args[]){ vehicle v=new vehicle();//call default constructor } }
When the above code is executed, it produces the following results
10 Motor car
In the above program, when we call default constructor, the parameterized constructor is automatically called.
this keyword used in setters and getters method
Program 1
class My_detail{// Java class int age; //instance variable String name; void set_detail(int age,String name){ //set method with parameters -local variable this.age=age; this.name=name; } int get_detail() {//get method for age return this.age; } String get_detail_1() {//get method for name return this.name; } public static void main (String args[]){ My_detail my=new My_detail(); my.set_detail(34,"Jhon"); //Object for class System.out.println("age is:"+my.get_detail()); System.out.println("name is:"+my.get_detail_1()); //display statement - call get method } }
When the above code executes, it produces the following results
age is :34; name is : Jhon
There are other Java language keywords that are similar to this keyword
Usage of this statement in the Java language
Constructor overloading in Java
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…