Pages

Pages

Prove that this variable stores current object reference

Prove that this variable stores current object reference.

/* Prove that this variable stores current object reference. */

class A
{
int x,y;

A(){
x=5;
y=6;
}

A(int x,int y){
x=7;
y=8;
}

void m1(){
System.out.println("In m1");
System.out.println(x+"..."+y);

m2();
System.out.println("In m1 after m2 call");
System.out.println(x+"..."+y);
}

void m2(){
x=14;
y=15;

new B().m3(this);
}
}

class B
{
int p=5;
int q=6;

B(){
p=p+2;
q=q+3;
}

void m3(A a){
a.x=p+a.x;
a.y=q+a.y;

new C(this,a);
}
}

class C{
C(B b,A a){
b.p=a.x+2;
b.q=a.y+3;

a.x=b.p;
a.y=b.q=a.x+5;
}
}

class TVSCOReference  //This Variable Stores Current Object Reference.
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A(1,2);

System.out.println("a1,a2 values before m1 call");
System.out.println(a1.x+"..."+a1.y);
System.out.println(a2.x+"..."+a2.y);

a1.m1();
System.out.println("a1,a2 values after m1 call");
System.out.println(a1.x+"..."+a1.y);
System.out.println(a2.x+"..."+a2.y);
}
}

Compilation:
G:\netlojava>javac TVSCOReference.java

Execution:
G:\netlojava>java TVSCOReference

Out Put:
a1,a2 values before m1 call
5...6
0...0
In m1 5...6 In m1 after m2 call
23...28
a1,a2 values after m1 call
23...28
0...0

1 comment: