I have got this warning: non-varargs call of varargs method with inexact argument type for last parameter;

前端 未结 1 835
孤独总比滥情好
孤独总比滥情好 2021-02-01 13:21

This is my sample code where i am getting the warning.

Class aClass = Class.forName(impl);
Method method = aClass.getMethod(\"getInstance\", null);
item = (Prefe         


        
相关标签:
1条回答
  • 2021-02-01 13:34

    Well, the compiler warning tells you everything you need to know. It doesn't know whether to treat null as a Class<?>[] to pass directly into getMethod, or as a single null entry in a new Class<?>[] array. I suspect you want the former behaviour, so cast the null to Class<?>[]:

    Method method = aClass.getMethod("getInstance", (Class<?>[]) null);
    

    If you wanted it to create a Class<?>[] with a single null element, you'd cast it to Class<?>:

    Method method = aClass.getMethod("getInstance", (Class<?>) null);
    

    Alternatively you could remove the argument altogether, and let the compiler build an empty array:

    Method method = aClass.getMethod("getInstance");
    
    0 讨论(0)
提交回复
热议问题