Which overload will get selected for null in Java?

JavaParametersMethodsInvocation

Java Problem Overview


If I write this line in Java:

JOptionPane.showInputDialog(null, "Write something");

Which method will be called?

  • showInputDialog(Component parent, Object message)
  • showInputDialog(Object message, Object initialSelectionValue)

I can test it. But in other cases similar to this, I want to know what happens.

Java Solutions


Solution 1 - Java

The most specific method will be called - in this case

showInputDialog(Component parent, Object message)

This generally comes under the "Determine Method Signature" step of overload resolution in the spec (15.12.2), and in particular "Choosing the Most Specific Method".

Without getting into the details (which you can read just as well in the spec as here), the introduction gives a good summary:

> If more than one member method is both > accessible and applicable to a method > invocation, it is necessary to choose > one to provide the descriptor for the > run-time method dispatch. The Java > programming language uses the rule > that the most specific method is > chosen. > > The informal intuition is that one > method is more specific than another > if any invocation handled by the first > method could be passed on to the other > one without a compile-time type error.

Solution 2 - Java

In your particular case the more specific method will be called. In general, though, there are some cases where the method signature can be ambiguous. Consider the following:

public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        m.testNullArgument(null);
    }

    private void testNullArgument( Object o )
    {
        System.out.println("An Object was passed...");
    }

    private void testNullArgument( Integer i )
    {
        System.out.println("An Integer was passed...");
    }

    private void testNullArgument( String s )
    {
        System.out.println("A String was passed...");
    }
}

In this case, the compiler can't decide between the method that takes an Integer and the method that takes a String. When I try to compile that, I get

reference to testNullArgument is ambiguous, both method testNullArgument(java.lang.Integer) in testnullargument.Main and method testNullArgument(java.lang.String) in testnullargument.Main match

Solution 3 - Java

Neither. You'll get a compiler error asking you to clarify what method you want to call. You can do so by explicitly casting the first argument:

showInputDialog((Object) null, "Write something");

or

showInputDialog((Component) null, "Write something");

Update I should have known - never doubt Jon Skeet. The problem I've referred to above only occurs when it's impossible to determine which method is more specific. Here's a test case:

public class Test {

  public void doSomething(String arg1, Object arg2) {
    System.out.println("String, Object");
  }

  public void doSomething(Object arg1, String arg2) {
    System.out.println("Object, String");
  }

  public static void main(String[] args) {
    Test test = new Test();
    test.doSomething(null, null);
  }
}

The above will give a compiler error.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijn CourteauxView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaBill the LizardView Answer on Stackoverflow
Solution 3 - JavaChssPly76View Answer on Stackoverflow