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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:37:51+00:00 2026-06-05T21:37:51+00:00

If I run the following program: class Runit{ public static void main(String[] argsWut) throws

  • 0

If I run the following program:

class Runit{
    public static void main(String[] argsWut) throws Exception {
        String arg = "what?";
        Class[] parameters = { new Object().getClass() };
        Object[] args = { arg };
        System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args));
    }
};

I get the following on the command line:

true

On the other hand, if I modify the parameters line a little:

class Runit{
    public static void main(String[] argsWut) throws Exception {
        String arg = "what?";
        Class[] parameters = { arg.getClass() }; // changed a little here so it's a bit more dynamic --
        Object[] args = { arg };
        System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args));
    }
};

I get:

Exception in thread "main" java.lang.NoSuchMethodException: java.lang.String.equals(java.lang.String)
    at java.lang.Class.getMethod(Class.java:1605)
    at test.Runit.main(Runit.java:7)

From this one example it looks to me as though the getMethod method only works with exact parameters. Is there a way to get some form of a “best fit” method? e.g. If an exact match exists, it would return that method, but if no exact match exists, it can return any method that could accept my given arguments.

  • 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-05T21:37:53+00:00Added an answer on June 5, 2026 at 9:37 pm

    From the documentation for getMethod():

    To find a matching method in a class C: If C declares exactly one
    public method with the specified name and exactly the same formal
    parameter types
    , that is the method reflected. If more than one such
    method is found in C, and one of these methods has a return type that
    is more specific than any of the others, that method is reflected;
    otherwise one of the methods is chosen arbitrarily.

    (Emphasis mine.)

    What you are asking for is to have reflection perform overload resolution for you. And apparently it won’t. If you really need this functionality, you can either 1) give up on using reflection and invoke the method directly, or 2) if that’s not possible, look up the rules for overload resolution in Java (you could start here), use getMethods() to determine the available methods, and then perform overload resolution manually. Fun times, I know.

    Edit: As other answerers have pointed out, someone has already taken the time to do that for you. Cool!

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

Sidebar

Related Questions

When I run the following program: public class Test { public static void main(String[]
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following C# code: class Program { static public void Print(string toPrint) {
Hi I have following test code: class Program { static void Main(string[] args) {
Consider the following Safe program: internal class Safe { public static void SafeMethodWillNeverThrow() {
Suppose I have the following situation: 9 class Program 10 { 11 public static
I get the following error when I run a java program: Exception in thread
Given the following code sample: public class WeirdStuff { public static int doSomething() {
Please consider the following test program (using scala 2.9.0.1) object test { def main(args:Array[String])
I have following program in same file. I have synchronized the run() method. class

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.