这样做是让程序员引用方法或者属性更加方便:
如我们在编写Junit测试用例时,都要使用assertEquals这个方法,如果我们不静态导入
import static org.junit.Assert.*;那么所有的测试用例中的assertEquals方法都必须些为:
Assert.assertEquals,那么这就太冗余了。
因此多次重复使用建议静态导入,否则还有可能引起混淆,并且这样的做法并不符合面向对象的程序设计。
public class Comment {
public static ResourceBundle res = ResourceBundle.getBundle("comment");
private String author;
private String email;
private String body;
private String message;
public Comment() {
//NOP
}
public Comment(String body, String email, String author) {
this.body = body;
this.email = email;
this.author = author;
}
在另一个类中引用时
import static com.hui.javabean.Comment.res;
import static org.junit.Assert.*;
静态变量和方法都可以引用,类名后加点和方法或者变量