How to Work With this Keyword in Java


This keyword refers to the current object. Now let us understand how to use this keyword.This is used to clear the "confusion of shadow".

As per Wiki--

this
    Used to represent an instance of the class in which it appears. this can be used to access class members and as a reference to the current instance. The this keyword is also used to forward a call from one constructor in a class to another constructor in the same class.
I will try to implement the Wiki's argument about this keyword.

Say i have class--
public class Class1 {
int x,y;
public Class1(int x,int y) {
x=x;
this.y=y;
}
}
Now if you are using an upgraded editor like jDeveloper it will automatically point out a warning on line where it is defined-x=x;
Why ??
Well here java got confused ??
the variable x is a new variable or the global variable?? And assuming it is a local variable java will print 0 as an output.

But for secondline this.y=y will surely make java understand that it is refereing to the global variable.So it will print the correct value.

So importance of this keyword to avoid variable name conflict.

The this keyword helps in removing the ambiguity of references and allows to refer to the current instance within the running program.
-- key word this is used with variables as "this.variable" and specifies the number of member variable of this instance of that class

this keyword can also be used to pass the current object of the class.

Always remember this keyword can not be used in the static context . Java will throw an compilation error.

Now if there is no name conflict a scenario I have created....




public class Class1 {
int x,y;
public Class1(int a,int b) {
x=a;
this.y=b;
}

public static void main(String[] args) {
Class1 class1 = new Class1(30,40);
System.out.println(class1.x);
System.out.println(class1.y);

}
}
This will not throw any wrong value.But it will create problem when you call it second time.
like--

public class Class1 {
int x,y;
public Class1(int a,int b) {
x=a;
this.y=b;
}

public static void main(String[] args) {
Class1 class1 = new Class1(30,40);
System.out.println(class1.x);
System.out.println(class1.y);
Class1 class2 = new Class1(50,60);
System.out.println(class1.x);
System.out.println(class1.y);

}
}

The instance variables are set with the first set of values and not able to update with second set of values. so the above written code is only applicable if that is a singleton class.

Else the class has to be synchronized.otherwise if two class trying to acess the same method that time it will be a mess.

What does the this keyword do??
when called during runtime this is replaced by the calling object.So..
this.x=x becomes class1.x=x;
this.y=y becomes class1.y=y;
again if a call comes from Class2


this.x becomes Class2.x=x;
this.y becomes Class2.y=y;
This is done by object handler automatically.


The forth implementation could be- the chaining of constructor..Wow!! what is that?
Let me write a piece of code

class Class2
{
public Class2()
{
this(1);
System.out.println("First Constructor");
}
public Class2(int a)
{
this(1,2);
System.out.println("Second Constructor");
}

/**
* @param a
* @param B
*/
public Class2( int a, int b)
{
this("Testing chain");
System.out.println("Third Constructor");
}
public Class2(String s)
{
System.out.println("Fourth Constructor");
}


public static void main(String args[])
{
Class2 first = new Class2();
}
}

What will be the output

Fourth Constructor
Third Constructor
Second Constructor
First Constructor


How??
When we call the default constructor

Class2 first = new Class2();

control goes to the default method..

public Class2()
{
this(10);
System.out.println("First Constructor");
}
Here the first line says this(10)..this nothing but to call the Class2(int a)
so the control goes to

public Class2(int a)
{
this(1,2);
System.out.println("Second Constructor");
}
And so on..
After that it will come back to calling merthod and perform remaining activities.

{ 0 comments... read them below or add one }

Post a Comment