How can two objects call each other?

Discussion in 'Java' started by Alex Ochoa, Jan 21, 2005.

  1. Alex Ochoa

    Alex Ochoa Guest

    Here's my problem:
    I have an object A that instantiates both objects B and C, and I want B
    to get something from C. My compiler is complaining that B doesn't
    recognize C. I made sure C is a public object in A. Is there a clever
    way of fixing this?

    The only solution (which I don't like too much) is to simply make A do
    everything C is supposed to do, so that B gets its information from a
    public variable in A.
     
    Alex Ochoa, Jan 21, 2005
    #1
    1. Advertising

  2. Alex Ochoa

    Alex Ochoa Guest

    Ok, I got another idea which isn't too bad, which is to change the
    methods so that B gets it's information from C through A. I think I'll
    do that. Just out of curiosity, I'm still wondering if there's a trick
    to overcome this instantly (like using some magic word somewhere in the
    code to make B recognize C).
     
    Alex Ochoa, Jan 21, 2005
    #2
    1. Advertising

  3. Alex Ochoa

    Guest

    Could you post your code?
     
    , Jan 21, 2005
    #3
  4. Alex Ochoa

    Alex Ochoa Guest

    It's terribly long...
    the idea is a little like this:

    *************************************************************

    public class ClassA{

    private ClassB B;
    public ClassC C;

    A (File aFile){
    opens aFile and reads it, breaks up into strings;
    C = new ClassC(stringC);
    other stuff; // creates a RandomAccessFile called rAFile, while
    talking to C.
    B = new ClassB(stringB); // sends other strings to other methods
    that behave kinda like B
    stuff; // closes RandomAccessFile rAFile.
    }

    public void mix(Stuff stuff1){
    rAFile.write(stuff2) // stuff2 written is based on stuff1 coming in
    }
    }

    *************************************************************

    public class ClassB {

    instantiates many little objects;

    public ClassB (String aString){
    stuff; // manipulates aString to get an integerArray
    long[] longArray = new long[integerArray.length];
    for (i...bla bla) {
    longArray = C.getLong(integerArray);
    }
    A.mix(stuff); //based on information from longArray.
    }
    }

    *************************************************************

    public class ClassC{

    public ClassC(String aString){
    stuff; //creates a long array called anArray starting from
    aString;
    }

    public long getLong(int integer){
    return anArray[integer];
    }
    }
     
    Alex Ochoa, Jan 21, 2005
    #4
  5. Alex Ochoa

    anonymous Guest

    Alex Ochoa wrote:
    > Here's my problem:
    > I have an object A that instantiates both objects B and C, and I want B
    > to get something from C. My compiler is complaining that B doesn't
    > recognize C. I made sure C is a public object in A. Is there a clever
    > way of fixing this?
    >
    > The only solution (which I don't like too much) is to simply make A do
    > everything C is supposed to do, so that B gets its information from a
    > public variable in A.
    >

    Sounds like you want a controller pattern somewhere. A pseudo effect can
    be achieved by allowing class B to callback to A to get the reference to
    C, or to simply pass C to B's xtor.
     
    anonymous, Jan 21, 2005
    #5
  6. Alex Ochoa

    Paul Guest

    On Fri, 21 Jan 2005 08:50:20 +0100, anonymous <> wrote:

    > Alex Ochoa wrote:
    >> Here's my problem:
    >> I have an object A that instantiates both objects B and C, and I want B
    >> to get something from C. My compiler is complaining that B doesn't
    >> recognize C. I made sure C is a public object in A. Is there a clever
    >> way of fixing this?
    >> The only solution (which I don't like too much) is to simply make A do
    >> everything C is supposed to do, so that B gets its information from a
    >> public variable in A.
    >>

    > Sounds like you want a controller pattern somewhere. A pseudo effect can
    > be achieved by allowing class B to callback to A to get the reference to
    > C, or to simply pass C to B's xtor.



    Have not looked at your code but one way of doing this is simply
    pass B a reference to C. Instantiate B and C first, and then make sure
    B gets a reference to C. Like this:

    public class A {
    public static void main(String a[]) {
    B b = new B();
    C c = new C();
    b.setRefToC(c);
    b.getSomethingFromC();
    }
    }

    public class B {
    private C c;

    public B() {

    }
    public void setRefToC(C obj) {
    c = obj;
    }
    public void getSomethingFromC() {
    int something = c.getSomething();
    }
    }

    public class C {

    public C() {

    }
    public int getSomething() {
    return 1;
    }
    }

    Hope this helps. Cheers Paul
     
    Paul, Jan 21, 2005
    #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. Replies:
    7
    Views:
    3,728
    John Currier
    Sep 4, 2005
  2. Fernie
    Replies:
    9
    Views:
    1,009
    David Dorward
    Dec 5, 2004
  3. cppaddict
    Replies:
    4
    Views:
    485
    jeffc
    Oct 15, 2003
  4. Tian
    Replies:
    2
    Views:
    397
    Reinhold Birkenfeld
    Mar 20, 2005
  5. Replies:
    5
    Views:
    423
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 16, 2007
Loading...

Share This Page