Classes/Objects  «Prev  Next»


Lesson 6Types of Nested Classes
ObjectiveOutline the 4 types of nested classes in Java..

Types of Nested Classes

There are four types or flavors of nested classes in Java:
  1. Static nested class
  2. Inner class
  3. Local inner class
  4. Anonymous inner class
The distinctions among these four flavors are not evident at first sight, and it does not help matters that alternative terms are often substituted for them. To help clarify the confusion, we represent the four flavors of nested classes schematically in Figure 3-6:

Figure 3-6: Types of Nested Classes.
Figure 3-6: Types of Nested Classes.

Static Non-static Anonymous
Non - local Static Nested Class Inner Class (Not Possible)
Local (Not possible) Local inner class Anonymous Inner Class


  1. A non-local class is defined inside a class.(Regular Inner Class)
  2. A Method-local Inner Class is defined within a code block (either a 1) method, 2) constructor, or 3) initialization block)
  3. An anonymous Inner Class, does not provide the name of the class; you just define its body.
  4. A static class is qualified using the static keyword, whereas a non-static class does not use the static keyword with the class definition.

One structure that you can create using static nested classes is to have a static subclass extend a static parent class. The following link Static Inheritance gives an example of how this is accomplished.

Static Inheritance using static Nested Classes

You can define a class as a static member inside another class.
The following example demonstrates how a static subclass can extend a parent class that is also declared as static, provided both are contained within a regular class.
In this case 1) Sub and 2) Super are static nested classes of the RegularClass.
static class Sub extends Super and compiles.

package com.java.nestedclasses;
public class RegularClass {
 static class Super {
  void myMethod() throws Exception {
   throw new Exception("throw exception within static parent..");
  }
 }
 static class Sub extends Super {
  void myMethod() {
   System.out.println("My method");
  }
 }
 public static void main(String[] args) {
  new Sub().myMethod();
 }
}
/* 
Program Output 
My method
*/


Member Class Declarations

  1. Member class declarations describe nested classes that are members of the surrounding class.
  2. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes .
  3. Method declarations describe code that may be invoked by method invocation expressions.
  4. A class method is invoked relative to the class type; an instance method is invoked with respect to some particular object that is an instance of a class type.
  5. A method whose declaration does not indicate how it is implemented must be declared abstract.
  6. A method may be declared final, in which case it cannot be hidden or overridden.
  7. A method may be implemented by platform-dependent native code.
  8. A synchronized method automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a synchronized statement , thus allowing its activities to be synchronized with those of other threads.

To get you started, here is a question dealing with inner classes and interfaces.
Which of the following options can be a part of a correct inner class declaration
or a combined declaration and instance initialization ?
(Assume that SimpleInterface and ComplexInterface are interfaces.)
Select 2 options:
  1. private class C { }
  2. new SimpleInterface() { //valid code}
  3. new ComplexInterface(x) { //valid code}
  4. private final abstract class C { }
  5. new ComplexClass() implements SimpleInterface { }


Answer: a, b
Explanation:
c. You cannot pass parameters when you implement an interface by an anonymous class.
d. A final class can never be abstract.
e. The implements keyword is used only in a class definition and not during instantiation.