Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: calling a method from other object: thred

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default 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();

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

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

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Calling from one javabean object to other javabean object in a single class
    By kichkich in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2011, 07:20 AM
  3. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 AM
  4. Identifying calling object
    By frogfury in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2010, 05:13 PM
  5. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM