REGISTER FREE to Receive Instant Job

Saturday, 1 November 2014

this keyword in Java

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’:-
  • ‘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’ 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

1 comment:

Popular Posts

Powered by Blogger.

About Me

My photo

JobsAcid is a 'job search engine' to provide finding a job on the internet easier for the user. It has designed as huge selection of job offerings on the same search engine.