Inner Classes, aka Nested Classes

Contact Us or call 1-877-932-8228
Inner Classes, aka Nested Classes

Inner Classes, aka Nested Classes

Inner classes, also known as nested classes are classes defined within another class.

They may be defined as public, protected, private, or with package access.

They may only be used "in the context" of the containingclass (outer class, or enclosing class), unless they are marked as static.

  • The outer class can freely instantiate inner class objects within its code; they are automatically associated with the outer class instance that created them.
  • Code in some other class can instantiate an inner class object associated with a specific instance of the outer class if the inner class definition is public (and its containing class is public as well).
  • If the inner class is static, then it can be instantiated without an outer class instance, otherwise, the inner class object must be attached to an instance of the outer class.

Inner classes are used to (these uses overlap to some extent):

  • Create a type of object that is only needed within one class, usually for some short-term purpose.
  • Create a utility type of object that cannot be used elsewhere (which would allow the programmer to change it without fear of repercussions in other classes).
  • Create one-of-a-kind interface implementations (such as individualized event handlers).
  • Allow a sort of multiple inheritance, since the inner class may extend a different class than the outer class extends, and an inner class instance would have access to its private elements as well as the private elements of the outer class object it is attached to
  • Implement one-to-many relationships where the classes are tightly coupled (meaning that code for one or both of the classes needs access to many of the private elements of the other class) - the outer class would be the "one" side of the relationship, with the inner class being the "many" side.
  • Provide a specialized form of callback, with which a class may pass very limited access to some of its internal components. Note that:
    • The collections classes provide an iterator, a class that implements the Iterator interface to loop through the elements in the collection using hasNext and next methods.
    • Given that the internal structure of the collection may be complex, implementing the iterator as an inner class enables it to navigate the structure, while not exposing any other aspects of the collection to the outside world.

Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is).

Outer class code has free access to all elements in any of its inner classes, no matter what their access term.

An inner class compiles to its own class file, separate from that of the outer class (the name of the file will be OuterClassName$InnerClassName.class, although within your code the name of the class will be OuterClassName.InnerClassName); you cannot use the dollar sign version of the name in your code.

An inner class occupies its own memory block, separate from the outer class memory block.

An inner class may extend one class, which might be unrelated to the class the outer class extends.

An inner class can implement one of more interfaces, and, if treated as an instance of one of its interfaces, external code may have no knowledge that the object actually comes from an inner class.

Next