{"id":400,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=400"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Nested Classes","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=400","title":{"rendered":"Nested Classes"},"content":{"rendered":"<p><DIV><br \/>\n<DIV id=PageTitle>Nested Classes<\/DIV><br \/>\n<BLOCKQUOTE>The Java programming language allows you to define a class within another class. Such a class is called a <I>nested class<\/I> and is illustrated here:<br \/>\n<BLOCKQUOTE><PRE>class OuterClass {<br \/>\n    &#8230;<br \/>\n    class NestedClass {<br \/>\n        &#8230;<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>A nested class is a member of its enclosing class and, as such, has access to other members of the enclosing class, even if they are declared private. As a member of <CODE>OuterClass<\/CODE>, a nested class can be declared <CODE>private<\/CODE>, <CODE>public<\/CODE>, <CODE>protected<\/CODE>, or <I>package private<\/I>. (Recall that outer classes can only be declared <CODE>public<\/CODE> or <I>package private<\/I>.)<br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG>Terminology:<\/STRONG>&nbsp;Nested classes are divided into two categories: static and non-static. Nested classes that are declared <CODE>static<\/CODE> are simply called <I>static nested classes<\/I>. Non-static nested classes are called <I>inner classes<\/I>.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><br \/>\n<BLOCKQUOTE><PRE>class OuterClass {<br \/>\n    &#8230;<br \/>\n    static class StaticNestedClass {<br \/>\n        &#8230;<br \/>\n    }<br \/>\n    class InnerClass {<br \/>\n        &#8230;<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<H3 fmstyle=\"C-Head\">Why Use Nested Classes?<\/H3>There are several compelling reasons for using nested classes, among them:<br \/>\n<UL><br \/>\n<LI>It is a way of logically grouping classes that are only used in one place. <\/LI><br \/>\n<LI>It increases encapsulation. <\/LI><br \/>\n<LI>Nested classes can lead to more readable and maintainable code. <\/LI><\/UL><br \/>\n<P><B>Logical grouping of classes<\/B>\u2014If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such &#8220;helper classes&#8221; makes their package more streamlined. <\/P><br \/>\n<P><B>Increased encapsulation<\/B>\u2014Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared <CODE>private<\/CODE>. By hiding class B within class A, A&#8217;s members can be declared private and B can access them. In addition, B itself can be hidden from the outside world. <\/P><br \/>\n<P><B>More readable, maintainable code<\/B>\u2014Nesting small classes within top-level classes places the code closer to where it is used. <\/P><br \/>\n<H3 fmstyle=\"C-Head\">Static Nested Classes<\/H3>As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class \u2014 it can use them only through an object reference.<br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG>Note:<\/STRONG>&nbsp;A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><br \/>\n<P>Static nested classes are accessed using the enclosing class name: <\/P><br \/>\n<BLOCKQUOTE><PRE>OuterClass.StaticNestedClass<br \/>\n<\/PRE><\/BLOCKQUOTE>For example, to create an object for the static nested class, use this syntax:<br \/>\n<BLOCKQUOTE><PRE>OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<H3 fmstyle=\"C-Head\">Inner Classes<\/H3>As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object&#8217;s methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.<br \/>\n<P>Objects that are instances of an inner class exist <I>within<\/I> an instance of the outer class. Consider the following classes: <\/P><br \/>\n<BLOCKQUOTE><PRE>class OuterClass {<br \/>\n    &#8230;<br \/>\n    class InnerClass {<br \/>\n        &#8230;<br \/>\n    }<br \/>\n}<\/p>\n<p><\/PRE><\/BLOCKQUOTE>An instance of <CODE>InnerClass<\/CODE> can exist only within an instance of <CODE>OuterClass<\/CODE> and has direct access to the methods and fields of its enclosing instance. <SPAN id=figure:classes-inner.gif>The next figure<\/SPAN> illustrates this idea.<br \/>\n<CENTER><br \/>\n<P><IMG height=124 alt=\"An InnerClass Exists Within an Instance of OuterClass. \" src=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/figures\/java\/classes-inner.gif\" width=232 align=bottom><\/P><br \/>\n<P class=FigureCaption>An InnerClass Exists Within an Instance of OuterClass <\/P><\/CENTER><br \/>\n<P>To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: <\/P><br \/>\n<BLOCKQUOTE><PRE>OuterClass.InnerClass innerObject = outerObject.new InnerClass();<br \/>\n<\/PRE><\/BLOCKQUOTE>Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes). Both of these will be discussed briefly in the next section. <\/BLOCKQUOTE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nested Classes The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here: class OuterClass { &#8230; class NestedClass { &#8230; } } A nested class is a member of its enclosing class and, as such, has access to other members &hellip; <a href=\"https:\/\/www.strongd.net\/?p=400\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Nested Classes<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-400","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=400"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/400\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}