Friday, June 12, 2015

this () key word

11:57 PM

this () key word: 

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.

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 SumDemo1
{
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 ();
}
};

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Java Tutorials. All rights resevered. Designed by Templateism

Back To Top