954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

threads and objects

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

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 
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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

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)

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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.. :(

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

OK.. then how to pass this reference to another class ??

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

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
NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: