{"id":12247,"date":"2015-02-26T10:00:31","date_gmt":"2015-02-26T10:00:31","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=12247"},"modified":"2020-09-01T07:10:45","modified_gmt":"2020-09-01T07:10:45","slug":"innerclasses","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/innerclasses\/","title":{"rendered":"Inner Classes"},"content":{"rendered":"<p>We have discussed access modifiers, exceptions, features of Java 8 in previous Java related posts. We will discuss \u2018Inner classes\u2019 which is a part of SCJP\/OCJP exam objective in this post. Inner classes are part of the code that often stumps a person who is taking the SCJP\/OCJP exam.<\/p>\n<p><strong>Definition of Inner classes:<\/strong><\/p>\n<p>Inner classes are classes which are nested within the outer classes. Why is this done? This is done to promote good object oriented design principles. \u00a0\u00a0According to the Oracle documentation website, inner classes promote:<\/p>\n<ol>\n<li>\u201cEncapsulation<\/li>\n<li>Readable and maintainable code<\/li>\n<li>Logical grouping of classes which is used only in one place\u201d<\/li>\n<\/ol>\n<p>(Nested Classes)<\/p>\n<p>We will see how these points come into play by observing some examples below.<\/p>\n<p><strong>Types of Inner classes:<\/strong><\/p>\n<p>Inner classes can be categorized into four types and they are<\/p>\n<ol>\n<li>Inner classes<\/li>\n<li>Method-local inner class<\/li>\n<li>Anonymous inner class<\/li>\n<li>Static nested class<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>Inner classes:<\/strong><\/p>\n<p>We will discuss the \u201cplain\u201d inner class in this post. There are two crucial points when dealing with Inner classes.<\/p>\n<p><strong><em>Point 1: Since the inner class is also a member of the outer class, it will have access to all the methods and variables of outer class even if they are declared private.\u00a0 This is the point that reiterates the concept of encapsulation which will be dealt with in the next post.<\/em><\/strong><\/p>\n<p>Illustrating a small example, if an inner class is defined the following way:<\/p>\n<p>class OuterClass{<br \/>\nprivate int x=5;<br \/>\nclass Inner{<br \/>\n}<br \/>\n}<\/p>\n<p>Class Inner will have access to the private integer variable \u2018x\u2019.<\/p>\n<p><strong>Invoking inner classes:<br \/>\n<\/strong>\u00a0Next we will see how to invoke inner classes in our code. \u00a0This brings us to the next point.<\/p>\n<p><strong><em>Point 2: All inner classes exist only within an outer class. An instance of an inner class can only exist if an instance of outer class exists. Inner classes do not exist alone!<\/em><\/strong><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Consider this code which shows how to instantiate an inner class from a method in the outer class. Here an instance of the outer class is always available by means of the \u201cthis\u201d variable.<\/p>\n<p>This program instantiates the inner class and calls a method in the inner class which prints the message \u201cHello world\u201d to the console.<\/p>\n<pre>\/\/ Program to create an instance of inner class from the outer class<\/pre>\n<pre><strong>package<\/strong> whizlabs;\n<strong>public<\/strong> <strong>class<\/strong> example_inner {\n\/\/Outer class method\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n<strong>void<\/strong> add(){\ne<strong>xample_inner.eg <\/strong><strong>e1<\/strong><strong>=<\/strong><strong>this<\/strong><strong>.<\/strong><strong>new<\/strong><strong> eg();\/\/Inner class is instantiated\n<\/strong>e1.sample();\n\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre>\/\/Inner class<\/pre>\n<pre><strong>public<\/strong> <strong>class<\/strong> eg{\n<strong>\u00a0\u00a0 public<\/strong> <strong>void<\/strong> sample(){\n\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"Hello world\");\n\u00a0\u00a0\u00a0 }\n }<\/pre>\n<pre><strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String args[]){\n<strong>\/\/Outer class instantiation\n<\/strong>\u00a0 example_inner e=<strong>new<\/strong> example_inner();\n\u00a0 e.add();\n\u00a0\u00a0\u00a0\u00a0 }\n}<\/pre>\n<p>In the example above, the:<\/p>\n<p>Outer class is: example_inner<br \/>\nOuter class method: add()<br \/>\nInner class is: eg<br \/>\nInner class method: sample()<\/p>\n<p>This is the line of code that creates an instance of the inner class from the outer class.<\/p>\n<p><em>example_inner.eg e1=this.new eg();\/\/Inner class is instantiated<\/em><\/p>\n<p>The above code can also be re-written the following way as the \u201cthis\u201d variable is always present in the outer class (outside \u2018static\u2019 methods)<\/p>\n<p><em>eg e1=new eg();<\/em><\/p>\n<p>Note the way the inner class is instantiated,<\/p>\n<p><strong>\u00a0\u00a0\u00a0 <\/strong><em>this.new eg();<\/em><\/p>\n<p>The output of the program as expected is listed below:<\/p>\n<p>Hello world<\/p>\n<p>We saw plain regular inner classes in this post. We will explore more about inner classes in subsequent posts.<\/p>\n<h6>Bibliography<\/h6>\n<h6><em>Nested Classes<\/em>. (n.d.). Retrieved Feb 19, 2015, from Java Tutorial &#8211; Oracle documentation: http:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/nested.html<\/h6>\n<h6><\/h6>\n","protected":false},"excerpt":{"rendered":"<p>We have discussed access modifiers, exceptions, features of Java 8 in previous Java related posts. We will discuss \u2018Inner classes\u2019 which is a part of SCJP\/OCJP exam objective in this post. Inner classes are part of the code that often stumps a person who is taking the SCJP\/OCJP exam. Definition of Inner classes: Inner classes are classes which are nested within the outer classes. Why is this done? This is done to promote good object oriented design principles. \u00a0\u00a0According to the Oracle documentation website, inner classes promote: \u201cEncapsulation Readable and maintainable code Logical grouping of classes which is used only [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[934,1148],"class_list":["post-12247","post","type-post","status-publish","format-standard","hentry","category-java","tag-inner-classes","tag-ocpjp"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":1,"uagb_excerpt":"We have discussed access modifiers, exceptions, features of Java 8 in previous Java related posts. We will discuss \u2018Inner classes\u2019 which is a part of SCJP\/OCJP exam objective in this post. Inner classes are part of the code that often stumps a person who is taking the SCJP\/OCJP exam. Definition of Inner classes: Inner classes&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=12247"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12247\/revisions"}],"predecessor-version":[{"id":75993,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12247\/revisions\/75993"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=12247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=12247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=12247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}