calling a method from other object: thred
Hello friends I'm pretty new to this forum.. This is my very first post wish to stay with you guys here..
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..
Re: calling a method from other object: thred
first declare your getStick() method as public and static then you can call that method like chopStick.getStick();
Re: calling a method from other object: thred
Please use the Java Naming Conventions (class names start with an uppercase letter, method and variable names start with a lowercase letter) to avoid confusion.
Don't forget that you can pass an instance of an object to the Runnable when you create it if you give it (the Runnable subclass) a constructor parameter and a class variable to hold the object. Also, remember that inner classes (e.g. your Runnable subclass) have access to all the class variables (such as the ChopStick instance) of the enclosing class.
The technique which will be most appropriate for you depends on the application requirements. I'm not familiar with the modified Dining Philosophers problem or your particular approach to the solution, and you've not posted any code, so I can't be more specific.