Category: 

What is a Nested Class?

Article Details
  • Written By: Jessica Susan Reuter
  • Edited By: A. Joseph
  • Image By: ビッグアップジャパン
  • Last Modified Date: 15 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
In late 19th-century London, mail was delivered to residential addresses up to twelve times each day.   more...

September 28 ,  1924 :  Two US military planes complete the first flights around the world.  more...

In object-oriented programming (OOP), a nested class is a class that is completely contained within another class. A variety of computer languages have support for this object-oriented structure, including C++ and the Java programming language. If a nested class is not declared static, it can also be called an inner class. A class can contain any number of inner classes, and each inner class can also contain inner classes of its own. Nested classes can be very useful, but they can also make class structures complicated quickly, so it is best for them to be used as part of a careful plan so that they don't become needlessly complex.

A nested class is often helpful to encapsulate small groups of functions or variables that are useful to another specific class. For example, if class A contains a group of methods that only it uses, these methods can be grouped into inner class B. Methods inside the inner class have access to the outer class's variables and methods while maintaining their own variables or constants as necessary. A nested class can be used as either a functional or organizational construct; while it is useful to encapsulate functionality, it is equally useful as an organizational tool. Ideal use of inner classes will both group together related functionality and organize code for easier readability and comprehension.

Ad

Commenting should be done on a nested class just as it is done on the outer class; it is easy for beginners to confuse inner classes with methods and forget this. When commenting a nested class, it is best for it to be thought of as a top-level class and to comment it accordingly. Inner class comments also provide an opportunity for the programmer to clarify the inner class's functionality and the rationale behind its organizational scheme. Multiple inner classes can exist within one outer class, so it is especially important for the programmer to comment each one clearly.

If a nested class is not declared static, an instance of the outer class must be explicitly created before an instance of the inner class is created. For inner classes that are declared static, this is not true. A static nested class usually can be created with fewer keystrokes and might have a wider scope than a simple inner class, so static nested classes are useful for creating easy to access utility methods, such as factories or simple self-contained methods with wide applications.

It is often helpful to developers when nested classes are described using the Unified Modeling Language. The Unified Modeling Language annotates and illustrates class structure and behavior in an unambiguous fashion, often using diagrams. Nested classes are given independent descriptions from the outer classes in which they reside, so it is very easy for their functionality to be identified and analyzed. Although it is not required for a nested class to be annotated in this way, it is often better for programmers to err on the side of greater detail.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

exception 'Exception' with message 'error writing captcha: Duplicate entry '2147483647' for key 'PRIMARY'' in /ssd/www/wisegeek/public_html/_core/classes/public/Captcha.php:44
Stack trace:
#0 /ssd/www/wisegeek/public_html/_core/controls/public/ControlDiscussionPostBox.php(324): Captcha->createCaptcha()
#1 /ssd/www/wisegeek/public_html/framework/classes/Control.php(104): ControlDiscussionPostBox->preRender(false)
#2 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): Control->render()
#3 /ssd/www/wisegeek/public_html/tpl/default-nocustom-lu/pages/public/article/article.htm(526): Control->__toString()
#4 /ssd/www/wisegeek/public_html/framework/classes/Control.php(300): require('/ssd/www/wisege...')
#5 /ssd/www/wisegeek/public_html/framework/classes/Control.php(309): Control->requireTpl('pages/public/ar...', Object(PageArticleCom), true)
#6 /ssd/www/wisegeek/public_html/framework/classes/Control.php(131): Control->renderTpl('pages/public/ar...', Object(PageArticleCom))
#7 /ssd/www/wisegeek/public_html/framework/classes/FormDataControl.php(87): Control->renderTemplate()
#8 /ssd/www/wisegeek/public_html/framework/classes/Control.php(109): FormDataControl->renderTemplate()
#9 /ssd/www/wisegeek/public_html/framework/classes/ScriptPage.php(50): Control->render(false)
#10 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): ScriptPage->render()
#11 /ssd/www/wisegeek/public_html/framework/classes/Page.php(97): Control->__toString()
#12 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(443): Page->processRequest()
#13 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(7): PublicFrontController->renderPage()
#14 /ssd/www/wisegeek/public_html/index.php(11): PublicFrontController::run()
#15 {main}