Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can two objects call each other?

Reply
Thread Tools

How can two objects call each other?

 
 
Alex Ochoa
Guest
Posts: n/a
 
      01-21-2005
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.

 
Reply With Quote
 
 
 
 
Alex Ochoa
Guest
Posts: n/a
 
      01-21-2005
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).

 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      01-21-2005
Could you post your code?

 
Reply With Quote
 
Alex Ochoa
Guest
Posts: n/a
 
      01-21-2005
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[i] = C.getLong(integerArray[i]);
}
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];
}
}

 
Reply With Quote
 
anonymous
Guest
Posts: n/a
 
      01-21-2005
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.
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      01-21-2005
On Fri, 21 Jan 2005 08:50:20 +0100, anonymous <(E-Mail Removed)> 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
 
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
Two .exe which write in Visual C++ 6.0, can they call each other functions..?? looi_sookchan@yahoo.com C++ 5 08-16-2007 09:31 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM
How two modules call functions defined in each other? Tian Python 2 03-20-2005 10:31 AM
Two submit buttons, can each call its own action? Fernie HTML 9 12-05-2004 07:09 PM



Advertisments