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

Discussion in 'Java' started by Rohit Gupta, May 22, 2006.

  1. Rohit Gupta

    Rohit Gupta Guest

    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
    Rohit Gupta, May 22, 2006
    #1
    1. Advertising

  2. Rohit Gupta

    Bjorn Abelli Guest

    "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
    Bjorn Abelli, May 22, 2006
    #2
    1. Advertising

  3. "Rohit Gupta" <> wrote in message
    news:...
    >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/
    Matt Humphrey, May 22, 2006
    #3
  4. Rohit Gupta

    Rohit Gupta Guest

    >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
    Rohit Gupta, May 22, 2006
    #4
  5. Rohit Gupta

    Oliver Wong Guest

    "Rohit Gupta" <> wrote in message
    news:...
    > >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
    Oliver Wong, May 25, 2006
    #5
  6. Rohit Gupta

    Alex Hunsley Guest

    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...
    Alex Hunsley, May 25, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    43
    Views:
    1,286
    Keith Thompson
    Feb 6, 2006
  2. Asfand Yar Qazi
    Replies:
    0
    Views:
    258
    Asfand Yar Qazi
    Feb 24, 2006
  3. Ed
    Replies:
    6
    Views:
    1,254
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 2, 2007
  4. Bob
    Replies:
    3
    Views:
    1,396
  5. cisono
    Replies:
    10
    Views:
    167
    A. Sinan Unur
    Aug 27, 2005
Loading...

Share This Page