changing values of a class from an object which it has created

R

Rohit Gupta

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
 
B

Bjorn Abelli

...
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



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
M

Matt Humphrey

Rohit Gupta said:
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 (e-mail address removed) http://www.iviz.com/
 
R

Rohit Gupta

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
 
O

Oliver Wong

Rohit Gupta said:
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
 
A

Alex Hunsley

Rohit said:
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

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top