Java 1.1 has significantly simplified some practical structures by modifying the Java language specification. In those modifications, the most striking is the internal class and anonymous classes. If you use it, they make the program more easily understood and maintained. Let's take a look at how these features work, how to use them correctly, and how to avoid some common errors.
Internal classes, "internal classes" are classes in the internal declaration of another class. Starting from Java 1.1, you can declare another class in a class, which is very similar to the declaration fields and methods. Classes packaging the internal declarations are called "external classes". In fact, the Java language specification also allows you to do more, including: declaring a class in another class or an interface. Declare an interface in another interface or a class. Declare a class in a method. Class and interface declarations can be nested with any depth. Listing A is some blank declarations of classes and interfaces, which demonstrates these possibilities. Using an Import statement, you can omit the package name like any other standard class. In addition, in an external class, a simple name can be utilized to reference all internal classes and interfaces (see NEW statements in Listing A). Note that INTER2 is specified from the Inner2 from Method1, because Inner2 is on a different level. Table A summarizes the fully qualified name of each internal class and interface declared in List A. After using the Import statement, shorter forms can be used. Of course, in the external class, you can also omit the names of the external class. Name Class / Interface Inner1 MyPackage.inner1 Interface1 MyPackage.Interface1 INNER2 MyPackage.Interface1.inner2 interface2 mypackage.interface1.interface2 inner3 inner3 is LOCAL for Method1, so it cannot be accessed outside
The most natural application of internal classes is to declare the classes that are only internally used in another class, or the declaration is closely related to another class. As shown in List B, it is a simple implementation of a linked list. Since the Node class is usually only used within the range of LinkedList, it is best to declare the Node as an internal class of LinkedList. Access control modifiers suitable for class members also apply to internal classes; that is, internal classes can have Package, Protected, Private, and Public access, their semantics and normal semantics have no difference. Since Node is to be used outside LinkedList, it declares it as public. However, the modifier Static has a different meaning. When applied to internal classes, it declares that the class is the same semantic as other classes, that is, instantiate, and use it like a standard class. The only difference is that it has full access to all static members of the external class. Listing C shows a simple program that creates a linked list and print it to the standard output device.
Non-static internal classes If the internal class does not specify a Static modifier, fully access all members of the external class, including instance fields and methods. To achieve this, a non-static internal class stores an implicit reference to an externally class. Therefore, instantiating a non-static internal class requires a new statement of different grammar: .new This form of New statement requires an instance of the external class, enabling the internal class to be created in the context of that instance. Note List A declare several non-static internal classes, and instantiate them in Method1 with standard New statements. The reason can be done, because Method1 is an example method of the external class, so the new statement is implicitly implicitly performed in the context of an instance of the external class. The modified syntax is required only when an unstatic internal class is instantially an unstatic internal class outside or in the context of other objects. However, non-static internal classes have some limits. In particular, they cannot declare static initialization lists and static members unless they are in a constant field. In addition, the internal declaration of the method is not accessible to the local variables and parameters of the method unless they are initialized into final. An anonymous anonymous class is a class that cannot be named, so there is no way to reference them. You must declare them as part of the New statement when you are created. This should be used in another form of new statement, as shown below: New
More optimized interior and anonymous classes are the two excellent tools for Java 1.1 for us. They provide better packages, the result is to make the code more easily understand and maintain, so that the relevant classes can exist in the same source code file (this is due to internal classes), and avoiding a large number of programs Class (this is due to anonymous class).