Hey dear Friends...

I'm developing a code for an assignment given that find a solution for a modified Dining philosophers problem.. For that problem I had to create four classes. Philosopher , chopStick , samllBowl and bigBowl .. I implemented the class philosopher as Runnable.. And the problem is this.. As far as I know I have to write whatever I need to do with the thread within the run method.. I want to call methods of other classes which I created.. but I haven't created any objects of those class in the philosophers class.. all objects are created within the main class, main method... so how can I access the getStick() method defined within the chopStick class....? thank you in advance..

how can I access the getStick() method defined within the chopStick class

To access any part of an instance of a class, you need a reference to that instance.
Some where you create an instance:
AnObject aRef= new AnObject(); // create an instance and set reference to that object

then you need to make that reference available where you want to call its methods:
aRef.aMethod(); // call a method in the AnObject class

Edited 5 Years Ago by NormR1: n/a

To access any part of an instance of a class, you need a reference to that instance.
Some where you create an instance:
AnObject aRef= new AnObject(); // create an instance and set reference to that object

then you need to make that reference available where you want to call its methods:
aRef.aMethod(); // call a method in the AnObject class

Sorry..I didn't get it friend.. Can you explain by a example please..?

I did. That's what the following is:

aRef.aMethod(); // call a method in the AnObject class

You need a reference (aRef) to an instance of the class(AnObject) to call one of its methods(aMethod)

Edited 5 Years Ago by NormR1: n/a

I did. That's what the following is:

aRef.aMethod(); // call a method in the AnObject class

You need a reference (aRef) to an instance of the class(AnObject) to call one of its methods(aMethod)

SO here in my scenario what is your advice ? what I am suppose to do.. I cant create instance of chopstick class in philosopher class..

Pass the reference in the constructor or via a setter method.

well I'm sorry. I don't know if i'm bothering you.. But I feel what you say is the thing to do.. but I don't know how to do it.. :(

AClass1 anObj = new AClass1(); // create an instance of AClass1

AClass2 anObj2 = new AClas2(anObj); // pass reference to AClass1 via constructor

...
anObj2.setReference(anObj); // pass reference via a setter method

After either of the above, the AClass2 object will have a reference to the AClass1 object.

Here are two ways that I suggested to pass a reference to a class. The same can be used to pass the reference to any number of classes.

AClass2 anObj2 = new AClas2(anObj); // pass reference to AClass1 via constructor
...
anObj2.setReference(anObj); // pass reference via a setter method

Here are two ways that I suggested to pass a reference to a class. The same can be used to pass the reference to any number of classes.

AClass2 anObj2 = new AClas2(anObj); // pass reference to AClass1 via constructor
...
anObj2.setReference(anObj); // pass reference via a setter method

Thank you very much for your time.. I'll try to understand it.. thank you again

This article has been dead for over six months. Start a new discussion instead.