参考:http://blog.csdn.net/aaronsi/article/details/187322 和 http://openhome.cc/Gossip/JavaGossip-V1/InnerClass.htm
內部類別(Inner class)或「巢狀類別」(Nested class)。
非"static"的內部類別可以分為三種:成員內部類別(Member inner class)、區域內部類別(Local inner class)與匿名內部類別(Anonymous inner class)。
member inner classes: 要创建它的实例,必须有一个指向上层的引用,它不能含有static方法,可以有final的方法。
local inner classes: 只能被声明成abstract/final的或默认class;只能访问final/static的本地变量。local and anonymous 内部类不能被声明为private、public、protected or static。
anonymous inner classes: 匿名类不能含有构造函数。
static inner classes只有最上层的类或static类可以有,并且它只能访问static变量。
例子如下:
public class ATest{ int nonstatic_a = 3; static int static_b = 4; final int final_c = 5; public static void main(String[] args){ } public void test01(int i){ final int c=0; int d = 1; //③local inner classes只能被定义成abstract/final的;只能访问final/static的本地变量。 //Illegal modifier for the local class InnerClass_Local; only abstract or final is permitted class InnerClass_Local{ final int l= 0; //Cannot refer to a non-final variable d inside an inner class defined in a different method //③local inner classes只能被定义成abstract/final的;只能访问final/static的本地变量。 // int e = d;//Compile Error! int e = static_b; int f = final_c; //The field ll cannot be declared static; //static fields/methods can only be declared in static or top level types // static int ll=2; //Compile Error! // static void f1(){//Compile Error! // System.out.print(c); // } } } void fun2(){ } static class InnerClass2{ static int ll=2; int aa=3; //①static inner classes只有最上层的类或static类可以有,并且它只能访问static变量。 // int bb = nonstatic_a; //Compile Error! static class InnerClass5{ } } class InnerClass3{ //②member inner classes要创建它的实例,必须有一个指向上层的引用,它不能含有static方法。 //The member type InnerClass4 cannot be declared static; //static types can only be declared in static or top level types // static class InnerClass4{//Compile Error! // } final void inner_fun(){ } } final class InnerClass4{ void final_fun(){ } } }