this keyword in java
//case 1:-this
keyword in java
//differentiate
local variable and instance variable if those are declared with same name
package thiskeyword;
public class PrintName {
String fname;
String mname;
String lname;
public
PrintName(String fname, String mname, String lname) {
super();
this.fname = fname;
this.mname = mname;
this.lname = lname;
}
public void
display()
{
System.out.println("First
Name="+fname);
System.out.println("Middle
Name="+mname);
System.out.println("Last
Name="+lname);
}
public static void
main(String[] args) {
PrintName
pn=new PrintName("Govind", "Ballabh", "khan");
pn.display();
}
}
o/p:---------------------
First
Name=Govind
Middle
Name=Ballabh
Last
Name=khan
=====================================================================================
//case 2:-this
keyword in java
// Using this()
to invoke current class constructor
package thiskeyword;
public class PrintNameDemo {
String fname;
String mname;
String lname;
public
PrintNameDemo()
{
this("Govind","Ballabh","Khan");
}
public
PrintNameDemo(String fname,String mname,String lname)
{
this.fname=fname;
this.mname=mname;
this.lname=lname;
}
public void
display()
{
System.out.println("First
Name="+fname);
System.out.println("Middle
Name="+mname);
System.out.println("Last
Name="+lname);
}
public static void
main(String[] args) {
PrintNameDemo
pnd=new PrintNameDemo();
pnd.display();
}
}
o/p:-----------------
First
Name=Govind
Middle
Name=Ballabh
Last Name=Khan
//case 3:-this
keyword in java
// to return the
current class instance
package thiskeyword;
public class ReturnCurrentObject {
String s1;
String s2;
public
ReturnCurrentObject(String s1, String s2) {
super();
this.s1 = s1;
this.s2 = s2;
}
ReturnCurrentObject
retObject()
{
return this;
}
public void
display()
{
System.out.println("s1="+s1);
System.out.println("s2="+s2);
}
public static void
main(String[] args) {
ReturnCurrentObject
rco=new ReturnCurrentObject("java", "Technology");
rco.display();
ReturnCurrentObject kk = rco.retObject();
kk.display();
//rco.retObject().display();
}
}
o/p:----------------------
s1=java
s2=Technology
s1=java
s2=Technology
========================================================================================
//case
4:-this keyword in java
// Using ‘this’
keyword as method parameter
package thiskeyword;
public class TestData {
int x;
int y;
int z;
public
TestData(int x, int y, int z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public void
display(TestData t)
{
System.out.println("Sum="+(x+y+z));
}
public void
get()
{
display(this);
}
public static void
main(String[] args) {
TestData
td=new TestData(50, 50, 100);
td.get();
}
}
o/p:-------------
Sum=200
=======================================================================================
//case 5:-this keyword in java
// Using ‘this’ keyword to invoke current
class method
package
thiskeyword;
public class
TestCheck {
String s1;
String s2;
public void
getdata(String x,String y)
{
s1=x;
s2=y;
this.showData();
}
public void
showData()
{
System.out.println("S1="+s1+"S2="+s2);
}
public static void
main(String[] args) {
TestCheck tc=new
TestCheck();
tc.getdata("govind", "khan");
}
}
o/p:--------------------
S1=govindS2=khan
======================================================================================
//case 6:-this keyword in java
Using ‘this’ keyword
as an argument in the constructor call
package thiskeyword;
public
class M {
N n;
public
M(N n) {
super();
this.n = n;
n.display();
}
}
package thiskeyword;
public
class N {
int
k=100;
public
N() {
M m=new
M(this);
}
void
display()
{
System.out.println("Value of k in Class N : " + k);
}
public
static void
main(String[] args) {
N obj
= new N();
}
}
o/p:-------------
//Value of k in Class N : 100
No comments:
Post a Comment