注意区分静态和非静态的区别
package one.two;
import java.lang.reflect.*;
public class StaticFieldRefactor
{
public static String name = "yinbodotcc";
public String notStatic = "notStaticValue";
public static void staticMethos(int intparam)
{
System.out.println("I am static methods");
}
public void notStaticMethos()
{
System.out.println("I am not a static methods");
}
public static void main(String[] args) throws Exception
{
Class klass = Class.forName("one.two.StaticFieldRefactor");
/**
* 如果不是public修饰这个静态常量,会出错
*/
Field fld = klass.getField("name");
System.out.println(fld.get(null));
fld.set(null, "cctodobniy");
System.out.println(fld.get(null));
System.out.println("******************************");
Field field = klass.getField("notStatic");
System.out.println(field.get(new StaticFieldRefactor()));
System.out.println("******************************");
Method staticMethos =klass.getMethod("staticMethos", new Class[]{int.class});
staticMethos.invoke(null, new Object[]{new Integer(1)});
System.out.println("******************************");
Method notstaticMethos =klass.getMethod("notStaticMethos", new Class[]{});
notstaticMethos.invoke(new StaticFieldRefactor(), new Object[]{});
}
}