Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

Java

Learn step by step



this keyword

this keyword in Java is a special keyword which can be used to represent current object or instance of any class in Java. “this” keyword can also call constructor of same class in Java and used to call overloaded constructor. In this Java tutorial we will see how to use this keyword in Java and different examples of this in Java. this sometime also associate with super keyword which is used to denote instance of super class in Java and can be used to call overloaded constructor in Java.

Important points about this keyword in Java

  • this keyword represent current instance of class.
  • You can synchronize on this in synchronized block in Java
  • this keyword can be used to call overloaded constructor in java. if used than it must be first statement in constructor this() will call no argument constructor and this(parameter) will call one argument constructor with appropriate parameter.
  • If member variable and local variable name conflict than this can be used to refer member variable.
  • this is a final variable in Java and you can not assign value to this.
  • you can call methods of class by using this keyword.
  • this can be used to return object. this is a valid return value.
  • this can be used to refer static members in Java as well but its discouraged and as per best practices this should be used on non static reference.
  • "this" keyword can not be used in static context i.e. inside static methods or static initializer block.

The this keyword can be used to refer current class instance variable.

If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.

Understanding the problem without this keyword

Let's understand the problem if we don't use this keyword by the example given below:


class student{
int id;
String name;

student(int id,String name){
id = id;
name = name;
}
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){
student s1 = new student(333,"Pankaj");
student s2 = new student(555,"Jay");
s1.display();
s2.display();
}
}

Output

0 null
0 null

In the above example, parameter (formal arguments) and instance variables are same that is why we are using this keyword to distinguish between local variable and instance variable.

Solution of the above problem by this keyword

//example of this keyword
class Student{
int id;
String name;

student(int id,String name){
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(333,"Pankaj");
Student s2 = new Student(555,"Jay");
s1.display();
s2.display();
}
}

Output

333 Pankaj
555 Jay

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 required

class Student{
int id;
String name;

student(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student e1 = new Student(333,"Pankaj");
Student e2 = new Student(555,"Jay");
e1.display();
e2.display();
}
}

Output

333 Pankaj
555 Jay
this() can be used to invoked current class constructor.

The this() constructor call can be used to invoke the current class constructor (constructor chaining). This approach is better if you have many constructors in the class and want to reuse that constructor.


/Program of this() constructor call (constructor chaining)

class Student{
int id;
String name;
Student (){System.out.println("default constructor is invoked");}

Student(int id,String name){
this ();//it is used to invoked current class constructor.
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){
Student e1 = new Student(333,"Pankaj");
Student e2 = new Student(555,"Jay");
e1.display();
e2.display();
}
}

Output

default constructor is invoked
default constructor is invoked
333 Pankaj
555 Jay
Where to use this() constructor call?


class Student{
int id;
String name;
String city;

Student(int id,String name){
this.id = id;
this.name = name;
}
Student(int id,String name,String city){
this(id,name);//now no need to initialize id and name
this.city=city;
}
void display(){System.out.println(id+" "+name+" "+city);}

public static void main(String args[]){
Student e1 = new Student(333,"Pankaj");
Student e2 = new Student(555,"Jay","delhi");
e1.display();
e2.display();
}
}

Output

333 Pankaj null
555 Jay delhi
The this keyword can be used to invoke current class method (implicitly).

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.


class S{
void m(){
System.out.println("method is invoked");
}
void n(){
this.m();//no need because compiler does it for you.
}
void p(){
n();//complier will add this to invoke n() method as this.n()
}
public static void main(String args[]){
S s1 = new S();
s1.p();
}
}

Output

method is invoked
this keyword can be passed as an argument in the method.

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


class S{
void m(S obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}

public static void main(String args[]){
S s1 = new S();
s1.p();
}
}

Output

method is invoked
The this keyword can be passed as argument in the constructor call.

We can pass the 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{
A obj;
B(A obj){
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of A class
}
}

class A{
int data=10;
A(){
B b=new B(this);
b.display();
}
public static void main(String args[]){
A a=new A();
}
}

Output

10
The this keyword can be used to return current class instance.

We can return the this keyword as an statement from the method. In such case, 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;
}

class A{
A getA(){
return this;
}
void msg(){System.out.println("Hello java");}
}

class Test{
public static void main(String args[]){
new A().getA().msg();
}
}

Output

Hello java


Related Videos