You are here

this keyword in java | Beginner to Advance guide

this-keyword-java

this keyword in java

This keyword may have a very high usage of Java. In Java, it is a reference variable that refers to the current object.

Java uses this keyword

Here are 6 uses of Java for this keyword.

  1. It also can be used to refer to the current class instance variable.
  2. It can be used to implement (in short) the current class method
  3. This () can be used to invoke the constructor of the current class.
  4. It can be passed as an argument in a method call.
  5. This can be passed as an argument to the constructor call.
  6. This method can be used to return the frequency of the current class.

Suggestion: If you start & beginner with Java, see only three uses of this keyword.

Why Is Digital Marketing Important?


1) this keyword in java: to reference the current class instance variable

This keyword can be used to refer to an instance variable of the current class. If the example is ambiguity between variables and parameters, then this keyword solves the ambiguity problem.

Understanding the problem without this keyword

If we do not use this keyword with the example below, then understand the problem:

class Student{  

int rollno;  

String name;  

float fee;  

Student(int rollno,String name,float fee){  

rollno=rollno;  

name=name;  

fee=fee;  

}  

void display(){System.out.println(rollno+” “+name+” “+fee);}  

}  

class TestThis1{  

public static void main(String args[]){  

Student s1=new Student(111,”ankit”,5000f);  

Student s2=new Student(112,”sumit”,6000f);  

s1.display();  

s2.display();  

}}  

Output:

0 null 0.0

0 null 0.0

In the above example, parameters (formal arguments) and instance variables are the same. So, we are using this keyword to distinguish local variables and instance variables.

Solution of the above problem by this keyword in java

class Student{  

int rollno;  

String name;  

float fee;  

Student(int rollno,String name,float fee){  

this.rollno=rollno;  

this.name=name;  

this.fee=fee;  

}  

void display(){System.out.println(rollno+” “+name+” “+fee);}  

}  

class TestThis2{  

public static void main(String args[]){  

Student s1=new Student(111,”ankit”,5000f);  

Student s2=new Student(112,”sumit”,6000f);  

s1.display();  

s2.display();  

}}  

Output:

111 Ankit 5000

112 sumit 6000

If the local variable (formal argument) and instance variable are different, there is no need to use this keyword like the following program:

Program where this keyword in java is not required

class Student{  

int rollno;  

String name;  

float fee;  

Student(int r,String n,float f){  

rollno=r;  

name=n;  

fee=f;  

}  

void display(){System.out.println(rollno+” “+name+” “+fee);}  

}  

class TestThis3{  

public static void main(String args[]){  

Student s1=new Student(111,”ankit”,5000f);  

Student s2=new Student(112,”sumit”,6000f);  

s1.display();  

s2.display();  

}}  

Output:

111 ankit 5000

112 sumit 6000

in addition, It is better to approach to use meaningful names for variables. So we use the same name for instance variables and parameters in real-time, and always use this keyword

2) this keyword in java: to invoke the current class method

You can apply the method of the current class using this keyword. If you don’t use this keyword, the compiler automatically adds this keyword while invoking the method. Let’s see the example

this keyword

class A{  

void m(){System.out.println(“hello m”);}  

void n(){  

System.out.println(“hello n”);  

//m();//same as this.m()  

this.m();  

}  

}  

class TestThis4{  

public static void main(String args[]){  

A a=new A();  

a.n();  

}}  

Output:

hello n

hello m

3) this() : to invoke current class constructor

The () constructor call in it can be used to invoke the current class constructor. It can also be used for the reuse of constructors. In other words, it also used for constructor chaining.

Calling default constructor from parameterized constructor:

class A{  

A(){System.out.println(“hello a”);}  

A(int x){  

this();  

System.out.println(x);  

}  

}  

class TestThis5{  

public static void main(String args[]){  

A a=new A(10);  

}}  

Output:

hello a

10

Calling parameterized constructor from default constructor:

class A{  

A(){  

this(5);  

System.out.println(“hello a”);  

}  

A(int x){  

System.out.println(x);  

}  

}  

class TestThis6{  

public static void main(String args[]){  

A a=new A();  

}}  

Output:

5

hello a

The real usage of this() constructor call

This () constructor call should be used to reuse constructor from constructor. It maintains the chain between constructors i.e. it is used for constructor chaining. Let’s look at the example below that demonstrates the actual use of this keyword.

class Student{  

int rollno;  

String name,course;  

float fee;  

Student(int rollno,String name,String course){  

this.rollno=rollno;  

this.name=name;  

this.course=course;  

}  

Student(int rollno,String name,String course,float fee){  

this(rollno,name,course);//reusing constructor  

this.fee=fee;  

}  

void display(){System.out.println(rollno+” “+name+” “+course+” “+fee);}  

}  

class TestThis7{  

public static void main(String args[]){  

Student s1=new Student(111,”ankit”,”java”);  

Student s2=new Student(112,”sumit”,”java”,6000f);  

s1.display();  

s2.display();  

}}  

Output:

111 ankit java null

112 sumit java 6000

Rule: Call to this() must be the first statement in constructor.

class Student{  

int rollno;  

String name,course;  

float fee;  

Student(int rollno,String name,String course){  

this.rollno=rollno;  

this.name=name;  

this.course=course;  

}  

Student(int rollno,String name,String course,float fee){  

this.fee=fee;  

this(rollno,name,course);//C.T.Error  

}  

void display(){System.out.println(rollno+” “+name+” “+course+” “+fee);}  

}  

class TestThis8{  

public static void main(String args[]){  

Student s1=new Student(111,”ankit”,”java”);  

Student s2=new Student(112,”sumit”,”java”,6000f);  

s1.display();  

s2.display();  

}}  

Output

Compile Time Error: Call to this must be first statement in constructor

4) this: to pass as an argument in the method

This keyword can also be passed as an argument in the method. It is mainly used in event handling. Let’s see the example:

class S2{  

  void m(S2 obj){  

  System.out.println(“method is invoked”);  

  }  

  void p(){  

  m(this);  

  }  

  public static void main(String args[]){  

  S2 s1 = new S2();  

  s1.p();  

  }  

}  

Output:

method is invoked

Application of this keyword in java that can be passed as an argument:

Event handling (or) in a situation where we have to reference one class to another. However, Also, it is used to reuse an object in many ways.


5) this keyword in java: to pass as an argument in the constructor call

We can pass this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let’s see the example:

class B{  

  A4 obj;  

  B(A4 obj){  

    this.obj=obj;  

  }  

  void display(){  

    System.out.println(obj.data);//using data member of A4 class  

  }  

}  

class A4{  

  int data=10;  

  A4(){  

   B b=new B(this);  

   b.display();  

  }  

  public static void main(String args[]){  

   A4 a=new A4();  

  }  

}  

Output : 10

6) this keyword can be used to return the current class instance

We can return this keyword as a statement from the method. But In such a case, the return type of the method must be the class type (non-primitive). Let’s see the example:

Syntax of this that can be returned as a statement

return_type method_name(){  

return this;  

}  

Example of this keyword that you return as a statement from the method

class A{  

A getA(){  

return this;  

}  

void msg(){System.out.println(“Hello java”);}  

}  

class Test1{  

public static void main(String args[]){  

new A().getA().msg();  

}  

}  

Output:

Hello java

Proving this keyword in java

Let’s prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and the output of both variables is the same.

class A5{  

void m(){  

System.out.println(this);//prints same reference ID  

}  

public static void main(String args[]){  

A5 obj=new A5();  

System.out.println(obj);//prints the reference ID  

obj.m();  

}  

}  

Output:

Leave a Reply

Top