Wednesday, May 29, 2013

"this" object and "this()" use in java with example



“this” object: 


o   “this” is an internal or implicit object created by the JVM for two properties: -

1)      It points/refers to the current class object.
2)       It is used to differentiate the data-members of the class and formal parameters of a method (must be available in the same class) if and only if they are similar. The data members of the class will be preceded with “this” keyword.
class A
{          
Int x,y;
Void add(int x,int y)
{
this.x=x;
this.y=y;
System.out.println(this.x);
System.out.println(this.y);
}
};
class B
{
public static void main(String args[])
{
A obj=new A();
obj.add(10,20);
}
}

NOTE: -We are calling the instance variable of the class without using the object in the parent class then we use
“this” keyword.

this() constructors:
According to java there are two types of this constructor

1)      this() - It is used to call the default constructor of the same class from the Parameterized constructor of the same class.
2)       this(…) - It is used to call the parameterized constructor from the default constructor or parameterized constructor.

class A
{
            int x,y;
            void add()
            {
                }
}
class B
{
            public static void main(String args[ ])    
            {
                        A obj = new A();
}//In class A no constructor is defined but still the start is right because JVM creates its own constructor which //default constructor.
}

Example: -
class Test
{
            int a,b;
            Test()
            {
a=1;
                        b=2;
                        System.out.println("I m Default constructor of Test");
                        System.out.println("Value of a:"+a);
                        System.out.println("Value of b:"+b);
            }
            Test(int a)
            {
                        //this(10,50);
                        this.a = a;
                        b=a;
                        System.out.println("I m Single Para constructor of Test");
                        System.out.println("Value of a:"+this.a);
                        System.out.println("Value of b:"+b);
            }
            Test(int a,int b)
            {
                        this();
                        this.a= a;
                        this.b= b;
                        System.out.println("I m Double Para constructor of Test");
                        System.out.println("Value of a:"+this.a);
                        System.out.println("Value of b:"+this.b);
            }
            Test(Test obj)
            {
                        this(100,200);
                        Test t = (Test)obj;
                        a = obj.a;
                        b = obj.b;
                        System.out.println("I m OBECT Para constructor of Test");
                        System.out.println("Value of a:"+a);
                        System.out.println("Value of b:"+b);
            }
};

class This
{
            public static void main(String args[])
            {
                        Test t3 = new Test(10,20);
                        Test t2 = new Test(30);
                        Test t1 = new Test();
                        Test t4 = new Test(t1);
            }
}

Here, 4 objects are created and diff memory is used.
 ================================================================

No comments:

Post a Comment