import java.lang.reflect.Method;
public class ClassLoadingTester
{
public static void main(String a[]){
try{
ClassLoader loader=new CustomClassLoader();
Class c=loader.loadClass("AddClass");
loader=null;
Object o=c.newInstance();
Method m=c.getMethod("getTwoNumbers",new Class[]{String.class,String.class});
m.invoke(o,new Object[]{"2","3"});
}catch(Exception e){
e.printStackTrace();
}
}
}
This code snippet generates a warning when i compile in java. I think it pertains to this line,
Method m=c.getMethod("getTwoNumbers",new Class[]{String.class,String.class});
When I attempt to put <String>
like this new Clas开发者_JAVA百科s<String>[]{String.class,String.class}
It generates an error saying generic array type
I'm doing my code in BlueJ.
But when I leave the code as is it generates that irritating warning ^^.
I just want to get rid of that warning what should I do with that line of code?
You can't create arrays of generic types like Class<String>
, hence the error. So you don't have much choice other than adding a @SuppressWarnings("unchecked")
annotation before the statement to silence the warning.
Note that these warnings almost always have a reason, and silencing them this way regularly may easily result in runtime class cast exceptions! So always think three times before using @SuppressWarnings("unchecked")
- use it only when you are absolutely sure the cast is safe and there can be no negative consequences. In this case it looks safe to me to use it.
int t=10, a=7, b=52, d=97;
if((t + d) / 2 > b && (d - t) % 2 != 0)
if(a%2==0 || b%2==0)
System.out.println(--t + a);
else
System.out.println(--t - a);
else
System.out.println(t + ++a);
精彩评论