2. 静态内部类(static inner class):只能访问外部类的静态成员变量与静态方法,生成静态内部类对象的方式为:
OuterClass.InnerClass inner = new OuterClass.InnerClass();
例子:
class StaticInner
{
private static int a = 4;
public static class Inner
{
public void test()
{
System.out.println(a);
}
}
}
public class StaticInnerClassTest
{
public static void main(String[] args)
{
StaticInner.Inner inner = new StaticInner.Inner();
3. 成员内部类(member inner class):可以访问外部类的静态与非静态的方法与成员变量。生成成员内部类对象的方式为:
OuterClass.InnerClass inner = new OuterClass().new InnerClass();
例子:
class MemberInner
{
private int a = 4;
public class Inner2
{
private int a = 5;
public void doSomething()
{
System.out.println(MemberInner.this.a);
}
}