Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > changing values of a class from an object which it has created

Reply
Thread Tools

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

 
 
Rohit Gupta
Guest
Posts: n/a
 
      05-22-2006
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

 
Reply With Quote
 
 
 
 
Bjorn Abelli
Guest
Posts: n/a
 
      05-22-2006
"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



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      05-22-2006

"Rohit Gupta" <> wrote in message
news: oups.com...
>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 http://www.iviz.com/


 
Reply With Quote
 
Rohit Gupta
Guest
Posts: n/a
 
      05-22-2006
>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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      05-25-2006

"Rohit Gupta" <> wrote in message
news: oups.com...
> >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

 
Reply With Quote
 
Alex Hunsley
Guest
Posts: n/a
 
      05-25-2006
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, 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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments Krishna Python 4 03-07-2008 09:44 PM
DocumentBuilder object is not able to parse a XML String which has a nodename which contains forward slash! Ed Java 6 08-02-2007 03:29 PM
Changing which methods a class has by deriving from a template argument Asfand Yar Qazi C++ 0 02-24-2006 04:53 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
object "loses values" when other object created cisono Perl Misc 10 08-26-2005 11:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57