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 6229109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:30:30+00:00 2026-05-24T09:30:30+00:00

I would like to get a Method object similar to this: Method myMethod =

  • 0

I would like to get a Method object similar to this:

Method myMethod = MyClass.class.getDeclaredMethod("myDeclaredMethod",Arg1Class.class);

But! I would like compile time checking of the existence of the method “myDeclaredMethod”. I don’t actually need to dynamically choose the method, I just need a reference to it so I can pass it to another method… similar to the way C has function pointers. I’d like to do something like this:

#include <stdio.h>

void helloWorld() {
    printf("hello\n");
}

void runFunction( void (myfunc)() ) {
    myfunc();
}

int main() {
    runFunction(helloWorld);
    return 0;
}

Notice, if I mistype “helloWorld” in the call “runFunction(helloWorld)”, I get a compile time error. I want that same compile time error in Java, if possible.

  • 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-05-24T09:30:31+00:00Added an answer on May 24, 2026 at 9:30 am

    I’m afraid you can’t do that in Java.

    However, the usual way to achieve this in Java is to define an interface that will declare only one method, the one you want to “pass as argument”, and implement it in a class. Your class has to implement the method you are interested in. When you want to pass a “reference” to the method, you actually pass an instance of your interface. You can call your method on it.

    An example might explain this:

    interface Something {
        void doSomething();
    }
    
    class HelloWorld {
        void doSomething() {
            System.out.println("Hello world");
        }
    }
    
    class Main {
    
        void runFunc(Something s) {
            s.doSomething();
        }
    
        public static void main(String... args) {
            runFunc(new HelloWorld());
        }
    
    }
    

    The problem is, if you want to call another method you need to create a new class implementing Something

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

Sidebar

Related Questions

I would like to get a list of the wireless networks available. Ideally this
I would like to get all the instances of an object of a certain
I would like to implement something similar to a c# delegate method in PHP.
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to get data from from different webpages such as addresses of
I would like to get up-to-date information on Google's index of a website, and
I would like to get the path to the execution directory of a Windows
I would like to get the absolute position of an element in relation to
I would like to get some of your ideas about resource name / categorizing
I would like to get the last modified date of a remote file by

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.