In
Java there are four types of
nested class: ;Static • Static member class, also called
static nested classes They are declared static. Like other things in static scope (i.e.
static methods), they do not have an enclosing instance, and cannot access
instance variables and methods of the enclosing class. They are almost identical to non-nested classes except for scope details (they can refer to
static variables and methods of the enclosing class without qualifying the name; other classes that are not one of its enclosing classes have to qualify its name with its enclosing class's name). Nested
interfaces are implicitly static. ;Non-static / inner classes Inner class The following categories are called
inner classes. Each instance of these classes has a reference to an
enclosing instance (i.e. an instance of the enclosing class), except for local and anonymous classes declared in static context. Hence, they can implicitly refer to instance variables and methods of the enclosing class. The enclosing instance reference can be explicitly obtained via EnclosingClassName.this. Inner classes may not have static variables or methods, except for compile-time constant variables. When they are created, they must have a reference to an instance of the enclosing class; which means they must either be created within an instance method or constructor of the enclosing class, or (for member and anonymous classes) be created using the syntax enclosingInstance.new InnerClass(). • Member class They are declared outside a function (hence a "member") and not declared "static". • Local class These are classes that are declared in the body of a function. They can only be referred to in the rest of the function. They can use local variables and parameters of the function, but only one that are declared "final". (This is because the local class instance must maintain a separate copy of the variable, as it may out-live the function; so as not to have the confusion of two modifiable variables with the same name in the same scope, the variable is forced to be non-modifiable.) Can be very helpful for creation a class with generic type fields, where the type variables are defined in the method. •
Anonymous class These are local classes that are automatically declared and instantiated in the middle of an expression. They can only directly extend one class or implement one interface. They can specify arguments to the constructor of the superclass, but cannot otherwise have a constructor (however, this is not a limitation, since it can have an instance initializer block to perform any initialization). ==Programming languages==