内部类是在另一个类内部定义的类。实际上,内部类可以在几种情况下构造。定义为类成员的内部类可以在该类中的任何位置实例化。方法内部定义的内部类只能稍后在同一方法中引用。内部类也可以命名或匿名。
package org.nhooo.example.lang; public class InnerClassDemo { private Bean bean; /** * Inner class, the compiled class will be named InnerClassDemo$Bean.class */ class Bean { public int width; public int height; @Override public String toString() { return width + " x " + height; } } public InnerClassDemo() { Bean bean = new Bean(); bean.width = 100; bean.height = 200; this.bean = bean; } public Bean getBean() { return this.bean; } public static void main(String[] args) { InnerClassDemo inner = new InnerClassDemo(); System.out.println("inner.getBean() = " + inner.getBean()); } }