Method Overloading for null argument

前端 未结 7 868
悲哀的现实
悲哀的现实 2020-11-21 13:48

I have added three methods with parameters:

public static  void doSomething(Object obj) {
    System.out.println(\"Object called\");
}

public static  void d         


        
7条回答
  •  萌比男神i
    2020-11-21 14:28

    I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.

    Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.

提交回复
热议问题