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

Thread: [Asking] Store method in HashMap

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default [Asking] Store method in HashMap

    Hi everyone,

    I got trouble about storing method in HashMap. I got the example but there is no parameter in method, You can see the code below

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author user
     */
    public class PracticeMap {
        public static void main(String[] args) {
             Map<String, Method> methodMap = new HashMap<String, Method>();
            try {
                methodMap.put("help", PracticeMap.class.getMethod("showHelp"));
                methodMap.get("help").invoke(null);
     
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(PracticeMap.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(PracticeMap.class.getName()).log(Level.SEVERE, null, ex);
            }catch (IllegalAccessException ex) {
                Logger.getLogger(PracticeMap.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(PracticeMap.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(PracticeMap.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
     
        public static void showHelp() {
            System.out.println("Help me!!");
        }
    }

    I want to put parameter in showHelp method. But I don't how to that.

    Please share me information if you already know


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Asking] Store method in HashMap

    Are you trying to use the reflection classes and methods to call a method in an object?

    What happens when the code is execute?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: [Asking] Store method in HashMap

    Hi norm,
    Yes, I use it
    The code will print Help me!!
    But it's difficult to use param on showHelp method. Please tell me if i did mistake on explanation

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Asking] Store method in HashMap

    Can you explain what mistake you think you made?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: [Asking] Store method in HashMap

    Hi Norm,
    Do you know about reflection in java ? I little don't understand about that.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Asking] Store method in HashMap

    One place to get info would be to read the API doc for the classes in the java.lang.reflect package.

    See the tutorial: http://docs.oracle.com/javase/tutori...ect/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: [Asking] Store method in HashMap

    Thank you Norm,

    Finally I got it

Similar Threads

  1. HashMap to Call a Method
    By AbarthGT in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2012, 01:30 PM
  2. Hashmap
    By ryan12345 in forum Java Theory & Questions
    Replies: 6
    Last Post: November 2nd, 2012, 07:04 PM
  3. How do i store input from the keyboard into a method?
    By javaStarter in forum Java Theory & Questions
    Replies: 2
    Last Post: December 23rd, 2011, 05:50 PM
  4. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  5. Replies: 2
    Last Post: April 21st, 2011, 10:29 AM