81: OVERVIEW OF NESTED TYPE DECLARATIONS in Java

Paint QR Code in Java 81: OVERVIEW OF NESTED TYPE DECLARATIONS
81: OVERVIEW OF NESTED TYPE DECLARATIONS
Printing QR Code In Java
Using Barcode generator for Java Control to generate, create QR Code image in Java applications.
of types that can be declared The Declaration Context column lists the lexical context in which a type can be declared The Accessibility Modifiers column indicates what accessibility can be specified for the type The Enclosing Instance column specifies whether an enclosing instance is associated with an instance of the type The Direct Access to Enclosing Context column lists what is directly accessible in the enclosing context from within the type The Declarations in Type Body column refers to what can be declared in the body of the type Subsequent sections on each nested type elaborate on the summary presented in Table 81 (N/A in the table means "not applicable")
Draw Barcode In Java
Using Barcode creator for Java Control to generate, create barcode image in Java applications.
Example 81
Scan Bar Code In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
Overview of Type Declarations
Generating QR Code ISO/IEC18004 In C#.NET
Using Barcode creator for .NET framework Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
class TLC { static class SMC {/**/} interface SMI {/**/} class NSMC {/**/} void nsm() { class NSLC {/**/} } static void sm() { class SLC {/**/} } SMC nsf = new SMC() { /**/ }; static SMI sf = new SMI() { /**/ }; enum SME {/**/} } // (1) Top level class // (2) Static member class // (3) Static member interface // (4) Non-static member (inner) class
Generating QR In .NET
Using Barcode creator for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
// (5) Local (inner) class in non-static context
QR Code Maker In VS .NET
Using Barcode encoder for .NET Control to generate, create QR-Code image in .NET framework applications.
// (6) Local (inner) class in static context
Drawing Denso QR Bar Code In Visual Basic .NET
Using Barcode maker for VS .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
// (7) Anonymous (inner) class in non-static context
Encoding EAN / UCC - 13 In Java
Using Barcode generator for Java Control to generate, create EAN 13 image in Java applications.
// (8) Anonymous (inner) class in static context
Painting Data Matrix In Java
Using Barcode maker for Java Control to generate, create ECC200 image in Java applications.
// (9) Static member enum
Printing Code 3/9 In Java
Using Barcode maker for Java Control to generate, create Code-39 image in Java applications.
Nested types can be regarded as a form of encapsulation, enforcing relationships between types by greater proximity They allow structuring of types and a special binding relationship between a nested object and its enclosing instance Used judiciously, they can be beneficial, but unrestrained use of nested types can easily result in unreadable code
Bar Code Maker In Java
Using Barcode generation for Java Control to generate, create bar code image in Java applications.
354 Table 81
Barcode Maker In Java
Using Barcode creation for Java Control to generate, create barcode image in Java applications.
CHAPTER 8: NESTED TYPE DECLARATIONS
ANSI/AIM Code 93 Maker In Java
Using Barcode drawer for Java Control to generate, create Code 93 Full ASCII image in Java applications.
Overview of Type Declarations Direct Access to Enclosing Context N/A
UCC-128 Generator In VS .NET
Using Barcode generator for .NET Control to generate, create USS-128 image in .NET framework applications.
Type Top-level Class, Enum, or Interface
Data Matrix ECC200 Maker In C#
Using Barcode maker for .NET framework Control to generate, create DataMatrix image in Visual Studio .NET applications.
Declaration Context Package
Making Barcode In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create bar code image in ASP.NET applications.
Accessibility Modifiers
Bar Code Maker In .NET
Using Barcode maker for Visual Studio .NET Control to generate, create barcode image in .NET applications.
public or
Generating Barcode In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create barcode image in VS .NET applications.
Enclosing Instance No
Encode Code 39 Extended In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create Code-39 image in VS .NET applications.
Declarations in Type Body All that are valid in a class, enum, or interface body, respectively All that are valid in a class, enum, or interface body, respectively Only non-static declarations + final static fields
Draw Data Matrix ECC200 In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create DataMatrix image in .NET applications.
default
Static Member Class, Enum, or Interface
As member of a toplevel type or a nested static type As non-static member of enclosing type In block with non-static context In block with static context
Static members in enclosing context
Non-static Member Class
All members in enclosing context
Local Class
None
All members in enclosing context + final local variables Static members in enclosing context + final local variables All members in enclosing context + final local variables
Only non-static declarations + final static fields Only non-static declarations + final static fields Only non-static declarations + final static fields
None
Anonymous Class
As expression in nonstatic context As expression in static context
None
None
Static members in enclosing context + final local variables
Only non-static declarations + final static fields
82: STATIC MEMBER TYPES
82 Static Member Types
Declaring and Using Static Member Types
A static member class, enum type, or interface comprises the same declarations as those allowed in an ordinary top-level class, enum type, or interface, respectively A static member class must be declared explicitly with the keyword static, as a static member of an enclosing type Nested interfaces are considered implicitly static, the keyword static can, therefore, be omitted Nested enum types are treated analogously to nested interface in this regard: they are static members The accessibility modifiers allowed for members in an enclosing type declaration can naturally be used for nested types Static member classes, enum types and interfaces can only be declared in top-level type declarations, or within other nested static members As regards nesting of types, any further discussion on nested classes and interfaces is also applicable to nested enum types In Example 82, the top-level class ListPool at (1) contains a static member class MyLinkedList at (2), which in turn defines a static member interface ILink at (3) and a static member class BiNode at (4) The static member class BiNode at (4) implements the static member interface IBiLink at (5) Note that each static member class is defined as static, just like static variables and methods in a top-level class
Example 82
Static Member Types
//Filename: ListPooljava package smc; public class ListPool { public static class MyLinkedList { private interface ILink { } public static class BiNode implements IBiLink { } } interface IBiLink extends MyLinkedListILink { } } // (1) Top-level class // (2) Static member class // (3) Static member interface
// (4) Static member class
// (5) Static member interface
//Filename: MyBiLinkedListjava package smc; public class MyBiLinkedList implements ListPoolIBiLink { // (6)
CHAPTER 8: NESTED TYPE DECLARATIONS ListPoolMyLinkedListBiNode objRef1 = new ListPoolMyLinkedListBiNode(); //ListPoolMyLinkedListILink ref; }
// (7) // (8) Compile-time error!
The full name of a (static or non-static) member class or interface includes the names of the classes and interfaces it is lexically nested in For example, the full name of the member class BiNode at (4) is ListPoolMyLinkedListBiNode The full name of the member interface IBiLink at (5) is ListPoolIBiLink Each member class or interface is uniquely identified by this naming syntax, which is a generalization of the naming scheme for packages The full name can be used in exactly the same way as any other top-level class or interface name, as shown at (6) and (7) Such a member s fully qualified name is its full name prefixed by the name of its package For example, the fully qualified name of the member class at (4) is smcListPoolMyLinkedListBiNode Note that a nested member type cannot have the same name as an enclosing type For all intents and purposes, a static member class or interface is very much like any other top-level class or interface Static variables and methods belong to a class, and not to instances of the class The same is true for static member classes and interfaces Within the scope of its top-level class or interface, a member class or interface can be referenced regardless of its accessibility modifier and lexical nesting, as shown at (5) in Example 82 Its accessibility modifier (and that of the types making up its full name) come into play when it is referenced by an external client The declaration at (8) in Example 82 will not compile because the member interface ListPoolMyLinkedListILink has private accessibility A static member class can be instantiated without any reference to any instance of the enclosing context, as is the case for instantiating top-level classes An example of creating an instance of a static member class is shown at (7) in Example 82 using the new operator If the file ListPooljava containing the declarations in Example 82 is compiled, it will result in the generation of the following class files in the package smc, where each file corresponds to either a class or an interface declaration:
ListPool$MyLinkedList$BiNodeclass ListPool$MyLinkedList$ILinkclass ListPool$MyLinkedListclass ListPool$IBiLinkclass ListPoolclass
Note how the full class name corresponds to the class file name (minus the extension), with the dollar sign ($) replaced by the dot sign () There is seldom any reason to import nested types from packages It would undermine the encapsulation achieved by such types However, a compilation unit can