Non-static method 'loadClass(java.lang.String)' cannot be referenced from a static context
1.今天在做类加载的时候竟然犯了这个小错误,所以记录一下
@Test
public void test8() throws ClassNotFoundException {
Class<?> clazz2 = ClassLoader.loadClass(“a”);
}
在一个类中调用另一个类中static方法或变量时要使用 类名.方法名或者类名.属性名;
如果调用的是非静态的方法或属性则要用类的对象去调用,即对象.方法或对象.属性。
正确写法:
@Test
public void test8() throws ClassNotFoundException {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?> clazz = classLoader.loadClass("class name");
}
还没有评论,来说两句吧...