this keyword-
In Java, each non-static method & constructor has an implicit parameter named this which holds the reference of the invoking object.
Program Test1 as understood by JRE is rewritten below:-
class Test1
{
int a, b;
public Test1(Test1 this, int a, int b)
{
a=a;
b=b;
}
public void display(Test1 this)
{
System.out.println("a = " + this.a);
System.out.println("b = " + this.b);
}
public static void main(String[] args)
{
Test1 t = new Test1(5, 6);
t.display();
}
}
t.display( );
display(t);
First usage of ‘this’:-
‘this’ keyword is used to identify data members of invoking objects in a method or constructor. In case, there is a conflict between object data member and local variables we use –
Syntax:-
this.<memberName>
Example Program -
class Test1
{
int a, b;
public Test1(int a, int b)
{
this.a=a;
this.b=b;
}
public void display()
{
System.out.println("a = " + this.a);
System.out.println("b = " + this.b);
}
public static void main(String[] args)
{
Test1 t = new Test1(5, 6);
t.display();
}
}
Output - a: 5 and b:6
Second usage of ‘this’:-
Then we will see how efficiently we can write the same program using ‘this’ keyword.
public class Swapper
{
int a, b;
public Swapper(int x, int y)
{
a = x;
b = y;
}
public void swap()
{
int c = a;
a = b;
b = c;
}
public void display()
{
System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String[] args)
{
Swapper x = new Swapper(5, 6);
x.display();
x.swap();
x.display();
}
}
Output:
a=5
b=6
a=6
b=5
In Java, each non-static method & constructor has an implicit parameter named this which holds the reference of the invoking object.
Program Test1 as understood by JRE is rewritten below:-
class Test1
{
int a, b;
public Test1(Test1 this, int a, int b)
{
a=a;
b=b;
}
public void display(Test1 this)
{
System.out.println("a = " + this.a);
System.out.println("b = " + this.b);
}
public static void main(String[] args)
{
Test1 t = new Test1(5, 6);
t.display();
}
}
t.display( );
display(t);
First usage of ‘this’:-
‘this’ keyword is used to identify data members of invoking objects in a method or constructor. In case, there is a conflict between object data member and local variables we use –
Syntax:-
this.<memberName>
Example Program -
class Test1
{
int a, b;
public Test1(int a, int b)
{
this.a=a;
this.b=b;
}
public void display()
{
System.out.println("a = " + this.a);
System.out.println("b = " + this.b);
}
public static void main(String[] args)
{
Test1 t = new Test1(5, 6);
t.display();
}
}
Output - a: 5 and b:6
Second usage of ‘this’:-
- ‘this’ keyword facilitate chaining of constructors of a class.
- Constructor Chaining is the facility in which one constructor of a class invokes another constructor of the same class.
Syntax:-
this(arguments if any)
NOTE: When ‘this’ keyword is used to invokes a constructor from another constructor, it must be the first statement in the invoking constructor.
Example Program -
public class ThisTestInMethod
{
int p, q;
public ThisTestInMethod()
{
this(2, 3);
System.out.println("Default");
}
public ThisTestInMethod(int x)
{
this(x, 3);
System.out.println("One Parameterized Constructor...");
}
public ThisTestInMethod(int x, int y)
{
p = x;
q = y;
System.out.println("Two Parameterized Constructor...");
}
public void display()
{
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("");
}
public static void main(String[] args)
{
ThisTestInMethod a = new ThisTestInMethod();
a.display();
ThisTestInMethod b = new ThisTestInMethod(10);
b.display();
ThisTestInMethod c = new ThisTestInMethod(30, 40);
c.display();
}
}
Output –
Two Parameterized Constructor...
p=2
q=3
Two Parameterized Constructor...
One Parameterized Constructor...
p=10
q=3
Two Parameterized Constructor...
p=30
q=40
Third usage of ‘this’:-
this(arguments if any)
NOTE: When ‘this’ keyword is used to invokes a constructor from another constructor, it must be the first statement in the invoking constructor.
Example Program -
public class ThisTestInMethod
{
int p, q;
public ThisTestInMethod()
{
this(2, 3);
System.out.println("Default");
}
public ThisTestInMethod(int x)
{
this(x, 3);
System.out.println("One Parameterized Constructor...");
}
public ThisTestInMethod(int x, int y)
{
p = x;
q = y;
System.out.println("Two Parameterized Constructor...");
}
public void display()
{
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("");
}
public static void main(String[] args)
{
ThisTestInMethod a = new ThisTestInMethod();
a.display();
ThisTestInMethod b = new ThisTestInMethod(10);
b.display();
ThisTestInMethod c = new ThisTestInMethod(30, 40);
c.display();
}
}
Output –
Two Parameterized Constructor...
p=2
q=3
Two Parameterized Constructor...
One Parameterized Constructor...
p=10
q=3
Two Parameterized Constructor...
p=30
q=40
Third usage of ‘this’:-
- ‘this’ keyword facilitate method chaining.
- Method Chaining is the facility of invoking multiple methods on an object in a single statement.
Program (Swapper) to understand the 3rd usage of ‘this’. This is a Swapper program which has been written without using ‘this’ keyword.
Then we will see how efficiently we can write the same program using ‘this’ keyword.
public class Swapper
{
int a, b;
public Swapper(int x, int y)
{
a = x;
b = y;
}
public void swap()
{
int c = a;
a = b;
b = c;
}
public void display()
{
System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String[] args)
{
Swapper x = new Swapper(5, 6);
x.display();
x.swap();
x.display();
}
}
Output:
a=5
b=6
a=6
b=5
Great and Useful Article.
ReplyDeleteJava Training Institutes in Chennai
Java Training in Chennai
Java Course in Chennai
Java Online Course
Online Java Training India
Java Online Course
Java Interview Questions
Core Java Interview Questions