Results 1 to 7 of 7
  1. #1
    skania is offline Member
    Join Date
    Jun 2013
    Posts
    15
    Rep Power
    0

    Default A method name stored as a string, can I call it to run the method?

    I have a basic game set up, and the quests will be linear. So I figured if I called the first quests from the main method, I can call subsequent methods from within. I chose to go with this, because I wanted to implement a save/load option. However, I would really not want a switch statement in the size of 50 cases by just storing the quest number, so I figured if I saved the method name, I would just call it from there and it would continue down the quest line.

    So is there any way I can take that string that holds the method's name, and then call that method?

    Thanks for any advice!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: A method name stored as a string, can I call it to run the method?

    So is there any way I can take that string that holds the method's name, and then call that method?
    You can do that via reflection, but it's really really not advisable to go down that path. Maybe runtime polymorphism would provide a better solution, but without knowing the details I can't be sure about that.

    I would really not want a switch statement in the size of 50 cases by just storing the quest number
    That looks to me like a candidate for refactoring using an array or List.

    Oh, and the main(...) method normally shouldn't contain any program logic. It should kick off the program, usually by creating an instance of a class (and optionally invoking a method on the created instance) and nothing more.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: A method name stored as a string, can I call it to run the method?

    You can call a method via its name using Reflection (Invoking Methods (The Java™ Tutorials > The Reflection API > Members)).

    However, I think refelection is the last mechanism you want to look at. Store some action that is identified by a name in a Map. Here's an example:

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    
    public class Adventure {
    
        private Map<String, Quest> quests;
    
        public Adventure() {
            quests = new HashMap<String, Quest>();
    
            quests.put("beginning", new BeginQuest());
            quests.put("end", new EndQuest());
        }
        
        public void showQuest(String name) {
            Quest q = quests.get(name);
            if (q == null) {
                throw new IllegalArgumentException("Quest " + name + " does not exist.");
            }
            q.showQuest();
        }
    }
    
    interface Quest {
        void showQuest();
    }
    
    class BeginQuest implements Quest {
    
        public void showQuest() {
            System.out.println("You find yourself in a pitch black room.");
        }
    }
    
    class EndQuest implements Quest {
    
        public void showQuest() {
            System.out.println("You die of dysentry.");
        }
    }
    Last edited by SurfMan; 06-21-2013 at 03:29 PM. Reason: Fixed weird part in example

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: A method name stored as a string, can I call it to run the method?

    Dammit db, don't type so fast! :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: A method name stored as a string, can I call it to run the method?

    Quote Originally Posted by SurfMan View Post
    Dammit db, don't type so fast! :)
    I don't provide tutorial links for an approach that I don't recommend ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    skania is offline Member
    Join Date
    Jun 2013
    Posts
    15
    Rep Power
    0

    Default Re: A method name stored as a string, can I call it to run the method?

    I'm only in my second week of Java, I think I'll implement the map - new experience can never hurt. Thanks for your input guys!

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: A method name stored as a string, can I call it to run the method?

    You're welcome. Please post new questions in a new post. We're happy to help! :)

Similar Threads

  1. call a method from another method in same class
    By rockstaedy in forum New To Java
    Replies: 5
    Last Post: 10-03-2012, 02:42 PM
  2. Replies: 2
    Last Post: 10-26-2011, 09:01 PM
  3. Replies: 4
    Last Post: 09-09-2011, 03:44 AM
  4. I can't get to call a method to another method..
    By arnelcolar in forum New To Java
    Replies: 2
    Last Post: 10-21-2009, 12:21 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •