Inner Classes Referenced from Outside the Enclosing Class

Contact Us or call 1-877-932-8228
Inner Classes Referenced from Outside the Enclosing Class

Inner Classes Referenced from Outside the Enclosing Class

If the access term for the inner class definition is public (or the element is accessible at package access or protected level to the other class), then other classes can hold references to one or more of these inner class objects

  • If the inner class is static, then it can exist without an outer class object, otherwise any inner class object must belong to an outer class instance.

For code that is not in the outer class, a reference to a static or non-static inner class object must use the outer class name, a dot, then the inner class name:

OuterClassName.InnerClassName innerClassVariable

If the inner class has an accessible constructor, you can you instantiate one from outside of the enclosing class, although the syntax is ugly, and there is rarely a need for this capability.

Next