Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:07:35+00:00 2026-06-06T03:07:35+00:00

I am trying to pass a method in as a parameter to a method

  • 0

I am trying to pass a method in as a parameter to a method in another class. The method is defined in the first class and the other class’s method is static. Seeing it will make it easier to understand:

Setup

public class MyClass extends ParentClass {
    public MyClass() {
        super(new ClickHandler() {
            public void onClick(ClickEvent event) {
                try {
                    OtherClass.responseMethod(MyClass.class.getMethod("myMethod",Boolean.class));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void myMethod(Boolean success) {
        if(success.booleanValue()) {
            //do stuff
        }
    }
}

When I try to build, though, I get the following error:

Error

The method getMethod(String, Class<boolean>) is undefined for the type Class<MyClass>

The problem isn’t that it’s not finding myMethod, it’s not finding Class<MyClass>.getMethod and I don’t know why.

Update

We’ve reworked this part of the code and are not using ‘getMethodorgetDeclaredMethod`. Since npe found a couple of problems with what I was doing plus put a lot of effort into finding the answer, I’m accepting that answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:07:36+00:00Added an answer on June 6, 2026 at 3:07 am

    Update 2

    The compile-time error suggests, that you are using Java 1.4 to compile the class.
    Now, in Java 1.4, it was illegal to define array parameters as Type..., you had to define them as Type[], and this is the way the getMethod is defined for the Class:

    Method getMethod(String name, Class[] parameterTypes)
    

    Because of that, you cannot use the simplified 1.5 syntax and write:

    MyClass.class.getMethod("myMethod",boolean.class));
    

    what you need to do is:

    MyClass.class.getMethod("myMethod",new Class[] {boolean.class}));
    

    Update 1

    The code you posted, does not compile because of another reason:

    super(new ClickHandler() {
    
        // This is anonymous class body 
        // You cannot place code directly here. Embed it in anonymous block, 
        // or a method.
    
        try {
            OtherClass.responseMethod(
                MyClass.class.getMethod("myMethod",boolean.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    

    What you should do is create a ClickHander constructor that accepts a Method, like this

    public ClickHandler(Method method) {
    
        try {
            OtherClass.responseMethod(method);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

    and then, in MyClass constructor invoke it like this:

    public MyClass() {
        super(new ClickHandler(MyClass.class.getMethod("myMethod",boolean.class)));
    }
    

    Original answer

    More to this, from the JavaDoc of Class#getMethod(String, Class...)

    Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

    And your method is private, not public.

    If you want to get access to private methods, you should rather use Class#getDeclaredMethod(String, Class...) and make it accessible by calling setAccessible(true).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm basically trying to pass a method to another class to be called later,
ERROR: unknown web method DoIt Parameter name: methodName I'm trying to pass a date
I'm trying to pass a list from one form class to another. Here's the
I am trying pass a Hashtable as parameter into a method which itself passes
How to pass a parameter through another bean's object inside a method called from
When trying to pass a CGPoint to a method the point is passed, but
I'm trying to pass a form's data to my controller method using JQuery Ajax,
I am trying to pass multiple options for UIView 's transitionWithView:duration:options:animations:completion: method. More specifically,
I am trying pass class as value in hashmap. I need to get the
Trying to pass a string argument to a function, which will then be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.