1
public class Class1 {

    public void method(Object obj){
        System.out.println("Object");
    }

    public void method(String str){
        System.out.println("String");
    }

    public static void main(String... arg){
        new Class1().method(null);
    }

}

as per JVM it will call to most specific argument type in this case most specific is String so method call happen to string argument type,but we need to call method of Object argument type

shmosel
  • 49,289
  • 6
  • 73
  • 138
amrutha
  • 19
  • 1

2 Answers2

2

You can cast null to string or object to call one of your methods null is the default value for String so it's call String method in your code

 new Class1().method((String)null);

 new Class1().method((Object)null);
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34
0

You can force the compiler to choose the method that takes an Object argument by casting the null to Object:

new Class1().method((Object)null);
Eran
  • 387,369
  • 54
  • 702
  • 768