{"id":1629,"date":"2011-03-11T00:49:27","date_gmt":"2011-03-10T19:19:27","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1629"},"modified":"2011-03-11T00:49:27","modified_gmt":"2011-03-10T19:19:27","slug":"nested-class-and-its-necessity-with-example","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/nested-class-and-its-necessity-with-example\/","title":{"rendered":"Nested Class and its necessity with example"},"content":{"rendered":"<p>The Java programming language allows you to define a class within another class. Such a class is called as &#8220;<strong>Nested class<\/strong>&#8221; and is illustrated here:<\/p>\n<figure id=\"attachment_1631\" aria-describedby=\"caption-attachment-1631\" style=\"width: 235px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Nested-Class-in-JAVA.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1631\" title=\"Nested Class in JAVA\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Nested-Class-in-JAVA.png?resize=235%2C240&#038;ssl=1\" alt=\"Nested Class in JAVA\" width=\"235\" height=\"240\" \/><\/a><figcaption id=\"caption-attachment-1631\" class=\"wp-caption-text\">Nested Class in JAVA<\/figcaption><\/figure>\n<p>Nested classes are divided into two parts: \u00a0&#8220;<strong>static Nested class&#8221;<\/strong> and &#8220;<strong>non \u2013 static nested class&#8221;<\/strong>.<\/p>\n<ol>\n<li><strong>Static Nested class <\/strong>: A nested class defined with keyword static is known as static Nested class.<\/li>\n<li><strong>Non Static Nested Class<\/strong> : Non static nested class is also known as &#8220;<strong>Inner Class&#8221;<\/strong>. Inner Classes are further divide into two types :\n<ul>\n<li><strong>Anonymous Class<\/strong> : A class which does not have any name is known as Anonymous Class.<\/li>\n<li><strong>Local Inner Class<\/strong> : A class defined within method is known as Local Inner Class.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<figure id=\"attachment_1632\" aria-describedby=\"caption-attachment-1632\" style=\"width: 457px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Types-of-Nested-Classes-in-JAVA.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1632   \" title=\"Types of Nested Classes in JAVA\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Types-of-Nested-Classes-in-JAVA.jpg?resize=457%2C199&#038;ssl=1\" alt=\"Types of Nested Classes in JAVA\" width=\"457\" height=\"199\" \/><\/a><figcaption id=\"caption-attachment-1632\" class=\"wp-caption-text\">Types of Nested Classes in JAVA<\/figcaption><\/figure>\n<p><!--more-->Below image shows that how to define the Nested class in JAVA:<\/p>\n<figure id=\"attachment_1633\" aria-describedby=\"caption-attachment-1633\" style=\"width: 398px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Defining-Nested-Class-in-Java.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1633\" title=\"Defining Nested Class in Java\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Defining-Nested-Class-in-Java.jpg?resize=398%2C508&#038;ssl=1\" alt=\"Defining Nested Class in Java\" width=\"398\" height=\"508\" \/><\/a><figcaption id=\"caption-attachment-1633\" class=\"wp-caption-text\">Defining Nested Class in Java<\/figcaption><\/figure>\n<p><strong>Static Nested Class :<\/strong><br \/>\nStatic nested classes <strong>do not have access to other members<\/strong> of the enclosing class.<\/p>\n<p><strong>Example :<\/strong> In JAVA API, Linked List class have declared &#8220;Entry&#8221; as static nested class.<\/p>\n<p>There is no need for <strong>LinkedList.Entry<\/strong> to be top-level class as it is only used by LinkedList (there are some other interfaces that also have static nested classes named Entry, such as Map.Entry &#8211; same concept). And since it does not need access to LinkedList&#8217;s members, it makes sense for it to be static &#8211; it&#8217;s a much cleaner approach.<\/p>\n<p>Static nested classes are accessed using the enclosing class name:<\/p>\n<blockquote><p>OuterClass.StaticNestedClass<\/p><\/blockquote>\n<p>For example, to create an object for the static nested class, use this syntax:<\/p>\n<blockquote><p>OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();<\/p><\/blockquote>\n<p><strong>Example :<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.StaticInnerClass;\n\/**\n * Example of Static Inner Class\n * @author jitendra Zaa\n *\n *\/\npublic class OuterClass {\n\n\tpublic static void main(String&#x5B;] args) {\n\t\tOuterClass.StaticNestedClass.display();\n\t}\n\n\tstatic class StaticNestedClass\n\t{\n\t\tstatic void display()\n\t\t{\n\t\t\tSystem.out.println(&quot;In Display of StaticNested Class&quot;);\n\t\t}\n\t}\n\n}\n<\/pre>\n<p><strong>Non Static Nested Class:<\/strong><br \/>\nA nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.<\/p>\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>\n<blockquote><p>OuterClass.InnerClass innerObject = outerObject.new InnerClass();<\/p><\/blockquote>\n<p><strong>Example :<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.InnerClass;\n\npublic class OuterClass {\nint b = 30;\n\tpublic static void main(String&#x5B;] args) {\n\t\tOuterClass.InnerClass obj = new OuterClass().new InnerClass();\n\t\tobj.display();\n\t}\n\n\tclass InnerClass {\n\t\tpublic void display()\n\t\t{\n\t\t\tSystem.out.println(&quot;In Display of non static Inner Class&quot;);\n\t\t}\n\t}\n}\n<\/pre>\n<p>Example demonstrating the simple difference in Static and non static nested class:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.InnerClass;\n\npublic class UpperClass {\npublic static class StaticInnerClass {}\n\npublic class InnerClass {}\n\npublic static void main(String&#x5B;] args) {\n        \/\/ works\n        StaticInnerClass stat = new StaticInnerClass();\n        \/\/ doesn't compile\n        InnerClass inner = new InnerClass();\n}\n}\n<\/pre>\n<p><strong> Local Inner Class :<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.LocalInnerClass;\n\npublic class OuterClass {\n\n\tpublic static void main(String&#x5B;] args) {\n\t\tOuterClass obj = new OuterClass();\n\t\tobj.method1();\n\t}\n\n\tclass LocalClass {\n\t\tpublic void display()\n\t\t{\n\t\t\tSystem.out.println(&quot;In Display of LocalClass - Outer&quot;);\n\t\t}\n\t}\n\n\tpublic void method1()\n\t{\n\t\t\/\/It Will call class outside the Method\n\t\tnew LocalClass().display();\n\n\t\tclass LocalClass {\n\t\t\tpublic void display()\n\t\t\t{\n\t\t\t\tSystem.out.println(&quot;In Display of actual LocalClass&quot;);\n\t\t\t}\n\t\t}\n\t\t\/\/It Will call Inner Local Class\n\t\tnew LocalClass().display();\n\t}\n}\n<\/pre>\n<blockquote><p>OutPut :<br \/>\nIn Display of LocalClass &#8211; Outer<br \/>\nIn Display of actual LocalClass<\/p><\/blockquote>\n<p><strong>Anonymous\u00a0Class:<\/strong><br \/>\nA class which does not have name is known as &#8220;<strong>Anonymous class&#8221;<\/strong>. These classes are mostly used in AWT event handlers. It is good to use the Anonymouse class, if it is not going to be used any where else.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.AnonymousClass;\npublic class ThreadSample {\n\n\tpublic static void main(String&#x5B;] args)\n\t{\n\t\tThread t = new Thread(\n\t\t\t\tnew Runnable(){\n\t\t\t\t\tpublic void run()\n\t\t\t\t\t{\n\t\t\t\t\t\tSystem.out.println(&quot;In run() method&quot;);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\tt.start();\n\t}\n}\n<\/pre>\n<p><strong>Why Use Nested Classes?<\/strong><br \/>\nThere are several compelling reasons for using nested classes, among them:<\/p>\n<ul>\n<li>It is a way of logically grouping classes that are only used in one place.<\/li>\n<li>It increases encapsulation.<\/li>\n<li>Nested classes can lead to more readable and maintainable code.<\/li>\n<\/ul>\n<p><strong>Logical grouping of classes&#8221;\u201d<\/strong> If 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>\n<p><strong>Increased encapsulation&#8221;\u201d<\/strong>Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. 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>\n<p><strong>More readable, maintainable code&#8221;\u201d<\/strong>Nesting small classes within top-level classes places the code closer to where it is used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example and Introduction of Nested classes in JAVA and there Types<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[329],"class_list":["post-1629","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":5389,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/responsive-datagrid-component-in-lightning-basics\/","url_meta":{"origin":1629,"position":0},"title":"Responsive Datagrid component in Lightning &#8211; Basics","author":"Jitendra","date":"March 15, 2016","format":false,"excerpt":"This blog post explains simple use case of Nested Lightning Component. It goes through example on how to interact and combine nested components to be used in parent Lightning component.","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Using Nested Lightning component to create responsive DataGrid","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Lightning-Data-Grid-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Lightning-Data-Grid-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Lightning-Data-Grid-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":5787,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/automated-code-review-for-apex-in-salesforce-static-code-analysis-video\/","url_meta":{"origin":1629,"position":1},"title":"Automated Code review for Apex in Salesforce &#8211; Static code analysis &#8211; Video","author":"Jitendra","date":"November 8, 2016","format":false,"excerpt":"Using open source PMD tool to generate code quality report for Apex classes","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Automated code review for apex in salesforce (static code analysis)","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5434,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/creating-custom-ant-plugin-or-task-in-java\/","url_meta":{"origin":1629,"position":2},"title":"Creating custom ANT plugin or Task in Java","author":"Jitendra","date":"March 19, 2016","format":false,"excerpt":"Tutorial to create custom Task in ANT with complete source code","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create custom ANT Task in Java","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":6848,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/lightning-web-component-event-handling-pub-sub\/","url_meta":{"origin":1629,"position":3},"title":"Lightning Web Component Event Handling &#8211; Pub Sub","author":"Jitendra","date":"June 21, 2019","format":false,"excerpt":"How to handle events in Lightning Web Components between nested and non nested Components","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Event Communication between Lightning Web Components","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/05\/Event-Communication-between-Lightning-Web-Components.png?fit=675%2C382&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/05\/Event-Communication-between-Lightning-Web-Components.png?fit=675%2C382&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/05\/Event-Communication-between-Lightning-Web-Components.png?fit=675%2C382&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1092,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/rtm-nu\/be-6th-sem-it-syllabus-of-rtmnu-nagpur-university-information-technology\/","url_meta":{"origin":1629,"position":4},"title":"BE 6th sem IT syllabus of RTMNU (Nagpur university &#8211; Information Technology)","author":"Jitendra","date":"September 15, 2010","format":false,"excerpt":"BE 6th sem IT syllabus of RTMNU (Nagpur university - Information Technology)","rel":"","context":"In &quot;RTM NU&quot;","block_context":{"text":"RTM NU","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/rtm-nu\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6133,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/text-slider-lightning-component-for-salesforce-with-live-demo\/","url_meta":{"origin":1629,"position":5},"title":"Text Slider Lightning Component for Salesforce  with Live Demo","author":"Jitendra","date":"June 5, 2017","format":false,"excerpt":"How to use Nested Components and create a simple yet powerful Text Slider Component in Lightning for Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Text Slider Lightning Component","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot-2017-06-04-at-9.29.26-PM.png?fit=1141%2C342&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot-2017-06-04-at-9.29.26-PM.png?fit=1141%2C342&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot-2017-06-04-at-9.29.26-PM.png?fit=1141%2C342&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot-2017-06-04-at-9.29.26-PM.png?fit=1141%2C342&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/06\/Screenshot-2017-06-04-at-9.29.26-PM.png?fit=1141%2C342&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1629"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}