- Java 9 Programming By Example
- Peter Verhas
- 728字
- 2021-07-02 23:37:34
Inner, nested, local, and anonymous classes
I have already mentioned inner and nested classes in the previous section. Now we look at them in bit more detail.
The details of inner and nested classes at this point may be difficult. Don't feel ashamed if you do not understand this section fully. If it is too difficult, skip to the next section and read about packages and return here later. Nested, inner, and local classes are rarely used, though they have their roles and use in Java. Anonymous classes were very popular in GUI programming with the Swing user interface that allowed developers to create Java GUI applications. With Java 8 and the lambda feature, anonymous classes are not so important these days, and with the emerging JavaScript and browser technology, the Java GUI became less popular.
When a class is defined in a file on its own, it is called a top-level class. Classes that are inside another class are, obviously, not top-level classes. If they are defined inside a class on the same level as fields (variables that are not local to some method or other block), they are inner or nested classes. There are two differences between them. One is that nested classes have the static keyword before the class keyword at their definition, and inner classes don't.
The other difference is that instances of nested classes can exist without an instance of the surrounding class. Inner class instances always have a reference to an instance of the surrounding class.
Because inner class instances cannot exist without an instance of the surrounding class, their instance can only be created by providing an instance of the outer class. We will see no difference if the surrounding class instance is the actual this variable, but if we want to create an instance of an inner class from outside the surrounding class, then we have to provide an instance variable before the new keyword separated by a dot, just like if new were a method. For example, we could have a class named TopLevel that has a class named InnerClass, like in the following code snippet:
public class TopLevel {
class InnerClass { }
}
Then we can create an instance of the InnerClass from outside with only a TopLevel object, like in this snippet:
TopLevel tl = new TopLevel();
InnerClass ic = tl.new InnerClass();
As inner classes have an implicit reference to an instance of the enclosing class, the code inside the inner class can access the fields and the methods of the enclosing class.
Nested classes do not have an implicit reference to any instance of the enclosing class, and they may be instantiated with the new keyword without any reference to any instance of any other class. Because of that, they cannot access the fields of the enclosing class unless they are static fields.
Local classes are classes that are defined inside a method, constructor, or an initializer block. We will soon talk about initializer blocks and constructors. Local classes can be used inside the block where they are defined.
Anonymous classes are defined and instantiated in a single command. They are a short form of a nested, inner, or local class, and the instantiation of the class. Anonymous classes always implement an interface or extend a named class. The new keyword is followed by the name of the interface or the class with the argument list to the constructor between parentheses. The block that defines the body of the anonymous class stands immediately after the constructor call. In the case of extending an interface, the constructor can only be the one without argument. The anonymous class with no name cannot have its own constructors. In modern Java we usually use lambda instead of anonymous classes.
Last but not least—well, actually, least I should mention that nested and inner classes can also be nested in deeper structures. Inner classes cannot contain nested classes, but nested classes can contain inner classes. Why? I have never met anyone who could reliably tell me the real reason. There is no architectural reason. It could be like that. Java does not permit that. However, it is not really interesting. If you happen to write code that has more than one level of class nesting then just stop doing it. Most probably you are doing something wrong.
- SoapUI Cookbook
- Python for Secret Agents:Volume II
- Reactive Programming with Swift
- 軟件測試項目實戰(zhàn)之性能測試篇
- Django Design Patterns and Best Practices
- 硅谷Python工程師面試指南:數(shù)據(jù)結(jié)構(gòu)、算法與系統(tǒng)設(shè)計
- Python深度學(xué)習(xí):模型、方法與實現(xiàn)
- D3.js By Example
- Julia for Data Science
- 軟件工程基礎(chǔ)與實訓(xùn)教程
- PyQt編程快速上手
- Penetration Testing with the Bash shell
- Instant GLEW
- 啊哈C語言?。哼壿嫷奶魬?zhàn)(修訂版)
- Spring Boot從入門到實戰(zhàn)