Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Run a method based on an integer

  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Question Run a method based on an integer

    OK very similar to switches & cases & ifs, but I'm wondering if I can do something like that:

    public Method[] method;

    method[0] = walk();
    method[1] = run();
    method[2] = stop();

    for (int i = 0; i < x; i++){
    if (i == myNumber) {
    run m[i];
    return;
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Run a method based on an integer

    You could use reflection and Method objects, but those are not worth the trouble since you're using those void methods that don't take any parameters; a Runnable implementation will do fine: in your class define a method array like this:

    Java Code:
    pubic Runnable[] method= {
       new Runnable() { public void run() { walk(); }},
       new Runnable() { public void run() { run(); }},
       new Runnable() { public void run() { stop(); }}
    };
    This code snippet assumes that you have also defined the run(), walk() and stop() methods in that class. Now you can call one of the Runnables (that calls one of the methods in your class as folllows:

    Java Code:
    method[i].run();
    The Runnable is used as a 'trampoline': you call the Runnable and it calls the wanted method.

    kind regards,

    Jos
    AlexGraal likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,810
    Rep Power
    5

    Default Re: Run a method based on an integer

    I think you have a recursive call in there. run calling itself. Might want to use a different interface. Or qualify the method calls with something like
    MyEnclosingClass.this.run();

    Regards,
    Jim
    Last edited by jim829; 06-01-2014 at 04:11 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Run a method based on an integer

    Quote Originally Posted by jim829 View Post
    I think you have a recursive call in there. run calling itself. Might want to use a different interface. Or qualify the method calls with something like
    MyEnclosingClass.this.run();
    Well, duh; so there ... I never run anyway,, so ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Run a method based on an integer

    Also one possibility is to have your own interface or super class. So you could have an Interface MovementAction with Implementations like Run, Walk, Jump, ...
    And then you call the method you defined in it (e.g. move()).

    That is more or less the same as Jos solution but you might have better names and maybe less confusion (So Runable is often/mostly/normaly used to start new threads.)

    With kind regards,

    Konrad

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Run a method based on an integer

    True, all true, but YAI (Yet Another Interface) only protects you from a name clash with the Runnable interface; if you know what you're doing (which obviously I'm not), (ab)using the Runnable interface wouldn't be a problem and it saves some typing which is nearly impossible in bright sunlight ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,810
    Rep Power
    5

    Default Re: Run a method based on an integer

    Of course, if one is adventuresome, and has Java 8 installed, then they could do the following:

    Java Code:
    public Runnable [] methods = {
             this::walk, // method reference
             this::run,
             this::stop
    };
    for (Runnable r : methods) {
          r.run();
    }
    Again, it assumes the methods exist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Run a method based on an integer

    Hi Jos,

    Your reply / suggestion was 100% ok and I agree fully.

    When I started my reply, I wanted to write more about a clean code point:
    Avoiding conditional statements and use other techniques e.g. polymorphism.

    But I simply realized that I either do it correct - which would have ended as an big article or keep the small forum reply length and keep to much out so that it simply must confuse a beginner.

    So while writing and removing my first reply I learned thay your reply was really good. A simple and complete solution using an existing Interface. All I could do was a simple extension because I had the feeling that it is an obvious point to us but a beginner mightend up using an interface because he thinks, that the interface must be used ...

    Konrad

Similar Threads

  1. Replies: 17
    Last Post: 07-17-2013, 10:45 AM
  2. Replies: 8
    Last Post: 07-05-2013, 08:13 AM
  3. Replies: 2
    Last Post: 01-31-2013, 07:37 PM
  4. Replies: 12
    Last Post: 10-31-2010, 07:14 PM
  5. Replies: 4
    Last Post: 03-25-2008, 01:16 PM

Posting Permissions

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