Reputation: 1364
Quick question, I tried to find a satisfactory answer on my own.
Say I have 2 classes Object1 and Object2. Now I want Object2 to do use Object1's resources so I do this
Object1 obj1;
public Object2(Object1 o){
obj1 = o;
}
and in Object1 I do this
new Object2(this);
Does this give each Object2
an Object1
, or does it simply point to the Object1 as expected?
Upvotes: 0
Views: 155
Reputation: 502
you can use IS-A relationship
Object2 extends Object1{ }
so now Object2 can use Object2 resources as well as Object1 resources. suppose you want vise versa, you can use HAS-A relationship
Object1 has Object2 reference like this
Object1{ Object2 obj2; }
Object2 has Object1 reference like this
Object2{ Object1 obj1; }
Upvotes: 0
Reputation: 4243
It will point to Object1
as expected. This is because Java stores object variables as "references"("pointers" if you like thinking in C) and, when you pass this reference by value, you are giving the function the objects reference.. There is a good basic tutorial on this functionality here. Hope this helps.
Upvotes: 1
Reputation: 181350
It simply uses a reference (points) to Object1. Whenever you change o
in Object2
class, you will be changing original obj1
object also.
Upvotes: 1