How can two objects call each other?

A

Alex Ochoa

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

Alex Ochoa

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

Alex Ochoa

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];
}
}
 
A

anonymous

Alex said:
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.
 
P

Paul

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
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top