this keyword in java

4 0 0
                                    

this keyword in java

There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.

Usage of java this keyword

Here is given the 6 usage of java this keyword.

this can be used to refer current class instance variable.this can be used to invoke current class method (implicitly)this() can be used to invoke current class constructor.this can be passed as an argument in the method call.this can be passed as argument in the constructor call.this can be used to return the current class instance from the method.

Suggestion: If you are beginner to java, lookup only three usage of this keyword.

1) this: to refer current class instance variable

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Understanding the problem without this keywordLet's understand the problem if we don't use this keyword by the example given below: 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 same. So, we are using this keyword to distinguish local variable and instance variable.

Solution of the above problem by this keywordclass 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 local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:

Program where this keyword is not requiredclass 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 It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.2) this: to invoke current class method

You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example

CC 104Where stories live. Discover now