No enclosing instance of type Reflection is accessible.

内部类是动态的,main方法是静态的`
package WebSever;
/*

  • 反射:把Java类中的各种结构(方法,属性等)反射成一个个Java对象
  • 1.获取class对象(三种方式)

*/
public class Reflection {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
//1.对象.getClass
IPhone p=new IPhone();
@SuppressWarnings(“rawtypes”)
Class clz=p.getClass();
//2.类.Class
clz=IPhone.class;
//3.Class.forName(“包名.类名”)
clz=Class.forName(“WebSever.IPhone”);
IPhone p2=(IPhone)clz.newInstance();
System.out.println(p2);

}
class IPhone{
public IPhone() {

}
}
如果将IPhone类写在内部将会出现错误如下:
No enclosing instance of type Reflection is accessible.
Must qualify the allocation with an enclosing instance of type Reflection (e.g. x.new A() where x is an instance of Reflection).
这时将类改为静态类即class IPhone前加static,这样可消除错误,或者将IPhone类写在外部。即

No enclosing instance of type Reflection is accessible.