Java Tutorial

This Keyword

'this': 'this' is an internal or implicit object created by JAVA for two purposes. They are

  1. 'this' object is internally pointing to current class object.
  2. Whenever the formal parameters and data members of the class are similar, to differentiate the data members of the class from formal parameters, the data members of class must be proceeded by 'this'.

this (): this () is used for calling current class default constructor from current class parameterized constructors.

this (...): this (...) is used for calling current class parameterized constructor from other category constructors of the same class.

For example:

class Test
{
    int a, b; Test ()
    {
        this (10); //calling current class single parameterized constructor
        System.out.println ("I AM FROM DEFAULT CONSTRUCTOR...");
        a=1;
        b=2;
        System.out.println ("VALUE OF a = "+a);
        System.out.println ("VALUE OF b = "+b);
    }
    Test (int x)	
    {
	this (100, 200); //calling current class double parameterized constructor
        System.out.println ("I AM FROM SINGLE PARAMETERIZED CONSTRUCTOR...");
        a=b=x;
        System.out.println ("VALUE OF a = "+a);
        System.out.println ("VALUE OF b = "+b);
    }
    Test (int a, int b)
    {
        System.out.println ("I AM FROM DEFAULT CONSTRUCTOR...");
        this.a=a;
        this.b=b;
        System.out.println ("VALUE OF a = "+this.a);
        System.out.println ("VALUE OF b = "+this.b);
        System.out.println ("VALUE OF a = "+a);
        System.out.println ("VALUE OF b = "+b);
    }
};
class TestDemo
{
    public static void main (String k [])
    {
        Test t1=new Test ();
    }
};

Rule for 'this':

Whenever we use either this () or this (...) in the current class constructors, that statements must be used as first statement only.

The order of the output containing this () or this (...) will be in the reverse order of the input which we gave as inputs.

For more clarity refer the above program

For example we need output as follows:

I AM FROM DEFAULT CONSTRUCTOR...	---1 VALUE OF a = 1
VALUE OF b = 2
I AM FROM SINGLE PARAMETERIZED CONSTRUCTOR... ---2 VALUE OF a = 100
VALUE OF b = 200
I AM FROM DOUBLE PARAMETERIZED CONSTRUCTOR... ---3 VALUE OF a = 10
VALUE OF b = 10

We must write in the following order as input:
Test (10);	---3
Test (100, 200);	---2
Test ();	---1
 

Note:

Whenever we refer the data members which are similar to formal parameters, the JVM gives first preference to formal parameters whereas whenever we write a keyword this before the variable name of a class then the JVM refers to data members of the class.

this methods are used for calling current class constructors.

Note:

If any method called by an object then that object is known as source object.

If we pass an object as a parameter to the method then that object is known as target object.

For example:

SOURCE OBJECT. METHOD NAME (TARGET OBJECT);
t1. display (t2); // written in main

In the definition of display method t1 data members are referred by this. Data member names (this. a & this. b) whereas t2 object data members are referred by formal object name. Data member names (T. a & T. b).

void display (Test T) //T is formal object member
{
    System.out.println ("VALUE OF a BELONGS TO DATA MEMBER ="+this.a);
    System.out.println ("VALUE OF b BELONGS TO DATA MEMBER ="+this.a);
    System.out.println ("VALUE OF a BELONGS TO FORMAL OBJECT MEMBER ="+T.a);
    System.out.println ("VALUE OF b BELONGS TO FORMAL OBJECT MEMBER ="+T.a);
}
            

Write a JAVA program which computes sum of two objects by accepting the data from command prompt?

Answer:

class Test
{
    int a,b; Test ()
    {
        a=b=0;
    }
    Test (int a, int b)
    {
        this.a=a;
        this.b=b;
    }
    Test sum (Test T)
    {
        Test T11=new Test ();
        T11.a=this.a+T.a;
        T11.b=this.b+T.b;
        return (T11);
    }
    void display ()
    {
    System.out.println ("VALUE OF a = "+a);
    System.out.println ("VALUE OF b = "+b);
    }
};
class SumDemo
{
    public static void main (String k[])
    {
        int n1=Integer.parseInt (k[0]);
        int n2=Integer.parseInt (k[1]);
        int n3=Integer.parseInt (k[2]);
        int n4=Integer.parseInt (k[3]);
        Test t1=new Test (n1,n2);
        Test t2=new Test (n3,n4);
        Test t3=new Test ();
        // t3=t1+t2; invalid statement t3=t1.sum (t2);
        System.out.println ("t1 VALUES ARE AS FOLLOWS..."); t1.display ();
        System.out.println ("t2 VALUES ARE AS FOLLOWS..."); t2.display ();
        System.out.println ("t3 VALUES ARE AS FOLLOWS...");
        t3.display ();
    }
};