Sunteți pe pagina 1din 6

this Keyword

this is a reference to the current object, within an instance method or a constructor the
object whose method or constructor is being called. We can refer to any member of the
current object from within an instance method or a constructor by using this.
this cannot be used with static methods.

Usage of this keyword


1. this keyword can be used to refer current class instance variable.
2. this() can be used to invoke current class constructor.
3. this keyword can be used to invoke current class method (implicitly)
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this keyword can also be used to return the current class instance.

1) 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.
In General if we have a class instance variable x of a data type y and we have a method in
which we pass another variable x of same data type to it as a parameter the compiler
wont be able to understand which variable x we are referring to. So we use this.
Eg 1)

Class Point

Class Point with this

public class Point {


public int x = 0;
public int y = 0;

public class Point {


public int x = 0;
public int y = 0;

//constructor
public Point(int x, int y)
{
x = x;
y = y;
}
public static void main(String args[]){
Point p=new Point(10,20);
System.out.println(p.x,p.y);
}
Ambiguity is created between class
instance x and parameter x (which x
should be assigned which value)

Eg2)

//constructor
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
Here this.x refers to class
instance x and it is assigned
the value of parameter x

public Loan(String type, double interest){


this.type = type;

this.interest =

interest;
}

2) this can be used to invoked current class constructor.


From within a constructor, you can also use the this keyword to call another constructor
in the same class. Doing so is called an explicit constructor invocation Eg 1)
class Loan{ private
double interest;
private String type;
public Loan(){
this(personal loan);
}
public Loan(String type){
this.type = type;
this.interest = 0.0;
}
}
Eg 2)
class Square
{
int
height;
int
width;
Square()
{
this(0,0);
}
Square(int side)
{
this(side, side);
}
Square(int height, int width)
{

this.height = height;
this.width = width;
}
}
class ImplSquare
{
public static void main(String args[])
{
Square sObj = new Square(4,6);
System.out.println("Variable values of object : ");
System.out.println("Object height = " + sObj.height);
System.out.println("Object width = " + sObj.width);
}
}

Output
Variable values of object :
Object height = 4
Object width = 6
In the above constructor this is used in a constructor to invoke the constructor of same class
in case of constructor overloading. We can do this by using this keyword followed by the list
of parameters, it is similar to invoking a method but only from within a constructor and
whenever it is used, it has to be the first statement in the constructor.

3)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 CheckInvocation{

void method1(){
System.out.println("method is invoked");
}
void method2(){
this.method1();
compiler does it for you.

//no need because

}
void method3(){
method2();

//complier will add this to invoke n() method as this.n()


}
public static void main(String args[]){
CheckInvocation s1 = new CheckInvocation ();
s1.method3(); //method3 calls method2 and it invoked and calls method1
}
}

4) 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.
class CheckInvocation{
voidmethod1(CheckInvocatin
obj){

System.out.println("method is invoked");
}
void method2(){
method1(this);
}
void method3(){
method2();
}
public static void main(String args[]){
CheckInvocation s1 = new CheckInvocation ();
s1.method3(); //method3 calls method2 and it invoked and calls method1
}
}

5) 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
For Eg.
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

6) 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): Syntax of this that can be returned as a
statement
return_type method_name(){
return this;
}
Eg,
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

Where Not to use this Keyword.


1) this is a final variable in Java and you can not assign value to this. this will result in
compilation error: this = new Loan();

//cannot assign value to final variable : this

2) this keyword can not be used in static context


i.e. inside static methods or static initializer block. if use this inside static
context you will get compilation error as shown below

public static void main(String args){


this.toString{};
//compilation error: non static variable this can not be used in static context
}

S-ar putea să vă placă și