1. Ordinary inner class (inner class non-static)
An inner class can access all members of a perimeter object, and when a perimeter class object creates an inner class object, the inner class object captures a reference to the perimeter class. Then, when accessing the members of the perimeter class, that reference is used to select the members of the perimeter class. The compiler could not access this reference without an error.
Ordinary inner classes cannot have static properties and methods, and nested classes are used if any.
Class Outer{public class Inner{}inner in = new Inner ()//contains the reference for this external object as This}public class Test{public static void Main (Stri Ng[] args) {//outer Outer = new Outer ();//outer.inner in = Outer.new Inner ();//These two sentences are exactly equivalent to the following outer.inner in = new Outer (). New Inner ();}}
Note: Outer.Inner in = new Outer (). New Inner (); When importing into a specific inner class, you can write Inner in = new Outer (). New Inner (); otherwise, take the outer class. In the new object, you must first obtain a reference to the object of the perimeter class.
2. Anonymous inner class
The following example combines the generation of the return value with the definition of the return value type, and he has no name. When a object is created, a is actually an interface and cannot be directly new to the object, so implementing an unassigned method is actually a class object that implements an interface and is then transformed upward into a reference to the parent class.
When using an anonymous inner class, the parameter reference must be final if the externally defined object is to be used internally.
Interface A{public void Method (); Class AImp implements A{public void Method () {System.out.println ("AImp");}} Class Test{private static a Geta () {//return new A () {//public void method () {//system.out.println ("aimp_test");//}//}; return new AIMP ();} public static void Main (string[] args) {Geta (). method ();;}}
3. Nested classes
You can declare an inner class as static if you do not need to have a connection between the inner class object and its outer object. A different inner class object implicitly saves a reference to an object outside of it. Nested classes do not require a perimeter class object when creating an object, but nested classes do not have access to non-static properties and methods in the perimeter class.
Broadcast receivers in Android are required to be statically nested classes when using static declarations, or the class cannot be found.
class Outer{private int x;public static class Inner{//int y = x;//error}inner in = new Inner ();} public class Test{public static void Main (string[] args) {Outer.Inner in = new Outer.Inner ();}}
The following use of handler in Android will warn Thishandler class should be static or leaks might occur,handler class should be static, otherwise memory leaks, The reason is that when you create the inner class of MyHandler, you also keep a reference to the external category, typically activity, which can be problematic once the activity is destroyed.
Class MyHandler extends Handler{public void Handlemessage (android.os.Message msg) {switch (msg.what) {}};}; Private Handler Handler = new MyHandler ();
4. The inner class is transformed upward into an interface--the exposed/hidden partial implementation
Class b{private void F () {System.out.println ("F ()");} private void G () {System.out.println ("G ()");} public class Aimp{public void Method1 () {f ();} public void Method2 () {g ();}}} public class Test{public static void Main (string[] args) {b b = new B (); AIMP AImp = B.new AImp (); Aimp.method1 (); Aimp.method2 ();}}
Since Aimp is public, it can be accessed and all of his methods can be accessed. The following interfaces are used to expose only the METHOD1 function. You can put the function you want to expose in the interface by turning it up into an interface.
Interface a{void method1 ();} Class b{private void F () {System.out.println ("F ()");} private void G () {System.out.println ("G ()");} Public A Geta () {return new AIMP ();} Private class AIMP implements A{public void Method1 () {f ();} public void Method2 () {g ();}}} public class Test{public static void Main (string[] args) {b b = new B ();//aimp AImp = B.new AImp ();//Because AIMP is private err OrA aImp = B.geta ();//B.geta () upward transformation, transforming into an interface where you can put the function you want to expose in the interface. Aimp.method1 (); Only exposed menthod1//aimp.method2 (); The method Method2 () is undefined for the type A}}
Java Internal classes