Place the class in the method class Outer {public void dosomething () {class inner{public void Seeouter () {}}}} (1), the method inner class can only be instantiated within the method that defines the inner class. It is not possible to instantiate it outside this method. (2), the method inner class object cannot use the non-final local variable of the method in which the inner class resides. Because the local variable of the method is on the stack, it only exists within the lifetime of the method. When a method ends, its stack structure is deleted, and local variables become history. But after the method finishes, the inner class object created inside the method may still exist in the heap! For example, if a reference to it is passed to some other code and stored within a member variable. Because there is no guarantee that the lifetime of local variables is as long as the inner class objects of the method, they cannot be used by inner class objects. The following is a complete example: class Outer {public void dosomething () {final int a =10;class inner{public void Seeouter () {System.out.println (a) ;}} Inner in = new Inner (); In.seeouter ();} public static void Main (string[] args) {Outer out = new Outer (); out.dosomething ();}} Anonymous inner classEditAs the name implies, there are no names inside the class. On the surface they seem to have names, which are not their names. When an anonymous inner class is used in a program, an object of that class is often created directly where the anonymous inner class is defined. The declaration format of the anonymous inner class is as follows: New ParentName () {...//internal class definition}[1] anonymous inner class is an inner class without a name. Under what circumstances do I need to use anonymous internal classes? Using an anonymous inner class is appropriate if some of the following conditions are true: • Use only one instance of the class. • Classes are used immediately after they are defined. • Classes are very small (sun recommends below 4 lines of code) • Naming your classes doesn't make your code easier to understand. There are several principles to keep in mind when using anonymous inner classes: • Anonymous Inner classes cannot have a constructor method. • Anonymous inner classes cannot define any static members, static methods. • Anonymous internal classes cannot be public,protected,private,static. Only one instance of an anonymous inner class can be created. • An anonymous inner class must be behind new, implementing an interface with its implication or implementing a class. • Because the anonymous inner class is a local inner class, all restrictions on the local inner class are applied to it. A, the anonymous inner class of the inheriting type public class Car {public void Drive () {System.out.println ("Driving a car!");} public static void Main (string[] args) {Car car = new Car () {public void Drive () {System.out.println ("Driving Another car!" );}}; Car.drive ();}} The result is output: Driving another car! The car reference variable is not a reference car object, but an object of the car anonymous subclass. B, an anonymous internal class of interfaces. Interface Vehicle {public void drive (); Class Test{public static void Main (string[] args) {Vehicle v = new Vehicle () {public void Drive () {System.out.println ("Drivi ng a car! ");}; V.drive ();}} The code above is strange, as if it were instantiating an interface. This is not the case, the anonymous inner class of an interface is an anonymous class that implements an interface. And only one interface can be implemented. C, an anonymous inner class of parameters. Class Bar{vOID Dostuff (Foo f) {F.foo ();}} Interface Foo{void Foo ();} Class test{static void Go () {Bar b = new Bar (); B.dostuff (new Foo () {public void Foo () {System.out.println ("Foofy");}});} Static nested classesEditStatic or non-static members can be defined in a statically intrinsic class. Technically, a static nested class does not belong to an inner class. Because an inner class shares a special relationship with an external class, it is more specifically a shared relationship to an instance. Static nested classes do not have the above relationship. It is only positioned inside another class and is therefore also referred to as a top-level nested class. The static meaning is that the inner class can access it just like any other static member without an external class object. Static nested classes can only access static members and methods of external classes. Class Outer{static class Inner{}}class Test {public static void main (string[] args) {Outer.Inner n = new Outer.Inner ();}} The inner class defined in the static method is also staticnested class, at this time cannot add the static keyword in front of the class, the Staticnested class in the static method is similar to the application of the inner class in the normal method, In addition to the direct access to static member variables in the outer class, it is possible to access local variables in the statically method, but the local variable must be added to the final modifier before it. Why internal classes are required typically, an inner class inherits from a class or implements an interface, and the inner class's code operations create objects of other peripheral classes. So you can assume that the inner class provides some kind of window into its outer class. The most appealing reason for using inner classes is that each inner class inherits itself from the implementation of one (interface), so no matter whether the perimeter class has inherited an implementation of (an interface), there is no effect on the inner class. Some design and programming problems can be difficult to solve without the ability of an inner class to inherit multiple concrete or abstract classes. From this point of view, the inner class makes the solution of multiple inheritance complete. The interface solves some of the problems, while the inner class effectively implements "multiple inheritance."
Java Internal classes