一言以蔽之。局部变量的名称不由编译器保留。作为快速实验,我使用Java 6和默认编译器选项编译了以下类:public class X { public static void main(String[] args) { int var = 2; System.out.println(var); }}快速检查生成的.class文件后发现,本地变量(var)的名称不在此处。
如果我的局部变量是实例变量和类变量,请按照以下步骤操作:String s = new String("This is a sample");Class<String> type = s.getClass();for ( Field f : type.getFields() ) { System.out.printf("Field %s is of type %s%n", f.getName(), f.getType().getName());}如果您的意思是方法/构造函数本地的变量,则不能通过反射访问它们。