---
title: "反射内部类"
author: "风沙星辰"
date: "2011-11-25T18:30:28Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-11-25T18:30:28Z"
id: 1278571
url: "https://jlib.iteye.com/blog/1278571"
---

```java

package test;

public class Outer {
	public class Inner{  
		public static final String aa="cc";
	}  
	
}

```



```java

package test;

import java.lang.reflect.Field;

public class Reflection {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Class c = null;
			//getDeclaredClasses获得所有内部类
//			Class[] cs = Outer.class.getDeclaredClasses();
//			
//			for(int i = 0 ; i < cs.length; i++){
//				
//				if(cs[i].getSimpleName().equals("Inner")){
//					c = cs[i];
//					break;
//				}
//			}
			c = Outer.class.forName("test.Outer$Inner");
			Field f = c.getDeclaredField("aa");
			f.setAccessible(true);
			System.out.println(f.get(null));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}

```