I have two classes A and B, A creates an object of B b_ob. A has a
private variable x. Now I need to change the value of x from the object
b_ob.
I hope the problem is clear, can someone suggest a way of doing so!!
Rohit
Bjorn Abelli - 22 May 2006 12:39 GMT
"Rohit Gupta" wrote...
> I have two classes A and B, A creates an object of B b_ob.
> A has a private variable x. Now I need to change the value
> of x from the object b_ob.
>
> I hope the problem is clear,
Only partially...
You ask "how" to do a specific thing technically, without telling us *why*
you want to do it. Bi-directional coupling is mostly a bad idea...
As an aside, newbies are usually better off in the group
comp.lang.java.help...
> can someone suggest a way of doing so!!
E.g.:
1. Let the instance of B get a reference
to the instance of A
This can be accomplished by e.g. providing the reference at instantiation of
the B object, or by providing it at a later stage, depending on how the
sequences of messages will go further on in the application.
2. Let there be a mutator (e.g. a public set-method)
for 'x' in the A class.
/// Bjorn A
Rohit Gupta - 22 May 2006 13:16 GMT
>You ask "how" to do a specific thing technically, without telling us *why*
>you want to do it. Bi-directional coupling is mostly a bad idea...
There are some methods in class B which uses x. I need to change the
value of x in one which can be reflected in the other.
>As an aside, newbies are usually better off in the group
>comp.lang.java.help..
I shall keep that in mind from now on.
Thanks for replying.
Rohit
Oliver Wong - 25 May 2006 18:25 GMT
> >You ask "how" to do a specific thing technically, without telling us
> >*why*
>>you want to do it. Bi-directional coupling is mostly a bad idea...
>
> There are some methods in class B which uses x. I need to change the
> value of x in one which can be reflected in the other.
I think Bjorn is expecting a higher level description that this. For
example, "I am trying to display an animation to the user which plays
whenever the application quits". Then Bjorn would be able to propose a
design which doesn't involve bi-directional coupling.
- Oliver
Matt Humphrey - 22 May 2006 12:46 GMT
>I have two classes A and B, A creates an object of B b_ob. A has a
> private variable x. Now I need to change the value of x from the object
> b_ob.
>
> I hope the problem is clear, can someone suggest a way of doing so!!
I think you already know that to change the value of x you simply assign it,
as in
x = ...
And because x is private to class A, the assignment must be done within one
of A's methods. Either A performs this operation itself or some other code
calls a method of A on an instance of A to do it. For some other code to
invoke the method, it must have a reference to an instance of A.
So the real questions are, what are you trying to assign to x? from where is
the assignment being made? does the source have a reference to an instance
of A?
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Alex Hunsley - 25 May 2006 23:53 GMT
> I have two classes A and B, A creates an object of B b_ob. A has a
> private variable x. Now I need to change the value of x from the object
[quoted text clipped - 3 lines]
>
> Rohit
As Bjorn notes, bi-directional coupling is not a good idea. You can
still get much the same result, but do it in a much better way, by using
the idea of a 'listener'.
In other words, class A creates class B. Class B offers a way for
interested parties to listen out for a certain something happening - A
registers itself as an interested party... then, when the interesting
thing happens (like some sort of change in B), class A gets notified,
and can inquire of B what state it is in.
If you want more details of this, google for "listener pattern" or
"subscriber/observer".
Telling us your requirement in more detail wouldn't hurt! Even if you
provided an example scenario, that would allow us to communicate more
clearly...