Here in this code snippet of Object Relation, I am taking reference of Class Two in Class One, and through it I am accessing class Two's members.
What if I create Class Two's Object in Class One itself and then access its methods? What will be the difference in doing it ?
I know I am missing some concept here but I am not getting it.
// Object Relation Using References
package Object_Relation;
class One {
// Instance Variables
int x;
Two t;
public One(Two t) {
this.t = t;
x=10;
}
void display() {
System.out.println("Class One Members : ");
System.out.println("x = "+x);
System.out.println("Displaying Class Two Members using its Method");
t.display();
System.out.println("Displaying Class Two Members using its reference :");
System.out.println("y = "+t.y);
}
}
class Two {
// Instance Variables
int y;
public Two(int y) {
this.y = y;
}
public void display() {
System.out.println("y = "+y);
}
}
public class UsingReference {
public static void main(String[] args) {
Two t2 = new Two(20);
One o = new One(t2);
o.display();
}
}