您在這裡

Scope Specifier

15 一月, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

When an embedded class is defined as static, it is called a nested class. The members (i.e. fields, methods, classes) of a (static) nested class can access only static members of the enclosing class.

When an embedded class is non-static, it is called an inner class. The members of an inner class can access ALL members of the enclosing class. The enclosing class (and its enclosing class, if any, and so on) contains the environment that completely defines the inner class and constitutes what is called the closure of the inner class. As all functional programmers should know, closure is a powerful concept. One of the greatest strength in the Java programming language is the capability to express closures via classes with inner classes. We shall see many examples that will illustrate this powerful concept in other modules.

Inner classes do not have to be anonymous as shown in the above examples. They can be named as well.