@SuppressWarnings的应用

 


@SuppressWarnings的应用



定义:

     @SuppressWarnings是 J2SE 提供的一个批注,该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。


 

单个方法中:

 下面是没有添加SuppressWarnings(“unchecked”):

@SuppressWarnings的应用      


添加@SuppressWarnings(“unchecked”)之后:

 @SuppressWarnings的应用



        黄线的警告突然没有了。恩恩,要@SuppressWarnings(“unchecked”)的作用就是这个。

 

 

在整个类中怎么设置?

        上面是单个的方法中,只要在相应的方法上面添加上@SuppressWarnings(“unchecked”),暂时没有到的变量的警告就可以去掉了,但是如果同一个类中有好几个需要去掉警告的方法,总不是说写好几个@SuppressWarnings(“unchecked”)吧,怎么做呢? 只要在类的上面添加一个@SuppressWarnings(“unchecked”)就行了。

 @SuppressWarnings的应用

 



@SuppressWarnings的应用




小结:

     在编码过程中,需要取消某些暂时的警告,在编译.java文件的时候,不在出现一些警告 ,如变量没有用到,会有提示警告,用@SuppressWarnings("unused")之后 ,就可以警告消失。