ramya narayanan wrote:We can create many instances of A & one particular instance has the state i as 10.
I want to use that A instance in my class B.
The obvious and simplest way is to pass a reference of the particular A instance into the B object.
Note however classes A & B don't have any direct relationship between them.
Only thing they both have same super class Panel.
Both extending the same class is not a useful relationship unless the root class keeps a static list of all instances created (and that's incredibly unlikely).
If B needs to know about a particular A instance then you need to have some form of relationship between them. This can be an indirect relationship eg B calls a method in C which returns the A instance or D calls a method in C which returns the A instance and then passes it to B but there has to be some sort of relationship. You can't think I know there's an A object somewhere in memory and so I'll just search the heap until I find it - this sort of thing just isn't allowed in
Java and for very good reasons.