Note two points in the members ' inner class
First: There can be no static variables and methods in the member's inner class;
Second: the member inner class is dependent on the outer class, so only the perimeter class is created first to be able to create the inner class.
Next is two examples (keywords:. this. New inner class hidden)
[. This] is used for internal classes to generate references to [external class objects that are currently creating the inner class]
[. New] used when an object of an external class must be used to create an inner class object
1 classTest001sub {2 Publictest001sub (String s) {3 System.out.println (s);4 }5 6 voidf () {7System.out.println ("Test001->f ()");8 }9 Ten //an inner class One Public classTest001inner { A Publictest001sub Callouter () { - returnTest001sub. This; - } the } - } - - Public classTest001 { + Public Static voidMain (string[] args) { -test001sub T1 =NewTest001sub ("New Test001");//New Test001 +Test001sub.test001inner T1inner = t1.NewTest001inner (); ATest001sub t2 =T1inner.callouter (); atT2.F ();//test001->f () -System.out.println (T1.equals (T2));//true - } -}
Protected and private access rules still apply to internal classes, and external references do not have access to protected and private objects (inner class shadowing).
The getinstance () method can be created inside an external class to obtain an inner class object. This method is generally recommended to create an inner class object.
1 InterfaceContents {2 intvalue ();3 }4 5 InterfaceDestination {6 String Readlabel ();7 }8 9 classTest002sub {Ten protected classTest002inner1ImplementsDestination { One PrivateString S; A - PrivateTest002inner1 (String s) { - This. S =s; the } - - @Override - PublicString Readlabel () { + returns; - } + } A at Private classTest002inner2ImplementsContents { - Private inti = 11; - - @Override - Public intvalue () { - returni; in } - } to + PublicTest002inner1 InstanceInner1 (String s) { - return NewTest002inner1 (s); the } * $ Publictest002inner2 InstanceInner2 () {Panax Notoginseng return NewTest002inner2 (); - } the } + A Public classTest002 { the Public Static voidMain (string[] args) { +Test002sub test002 =Newtest002sub (); -Destination test002impl1 = Test002.instanceinner1 ("Test002inner1"); $Contents TEST002IMPL2 =Test002.instanceinner2 (); $System.out.println (Test002impl1.readlabel ());//Test002inner1 -System.out.println (Test002impl2.value ());// One - //external protected and private objects cannot be accessed by reference the //Test002sub.test002inner1 a = Test002.new test002inner1 ("a"); - //test002sub.test002inner2 B = test002.new test002inner2 ();Wuyi } the}
Java Inner Class (2): normal member Inner class