{"id":12334,"date":"2015-03-11T10:00:58","date_gmt":"2015-03-11T10:00:58","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=12334"},"modified":"2020-09-01T07:10:25","modified_gmt":"2020-09-01T07:10:25","slug":"inner-classes-ii","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/inner-classes-ii\/","title":{"rendered":"Inner Classes &#8211; II"},"content":{"rendered":"<p>Continuing with our discussion of inner classes, this post will deal with how to create inner classes from outside the outer class instance code and also deal with the next type of inner class (inner classes declared inside methods)<\/p>\n<p>Recalling a few crucial points from last post, it should be remembered that<\/p>\n<ol>\n<li>An instance of inner class always exists only if an instance of outer class exists. Inner classes never exist alone.<\/li>\n<li>Inner classes always have access to all the methods and members of the outer class even if they are declared private.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>In the previous post, we learnt how to create an instance of inner classes from the outer class with an example. The code below will create an instance of inner class from outside the outer class instance code (and inside the static method):<\/p>\n<pre><strong>package<\/strong> whizlabs;\n\/\/Outer class\n<strong>public<\/strong> <strong>class<\/strong> outer {\n\u00a0\u00a0\u00a0\u00a0 <strong>private<\/strong> <strong>int<\/strong> i=6;\n\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ Inner class\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>class<\/strong> inner3{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0<strong>void<\/strong> simple(){\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(i);\u00a0\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String[] args) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Create an instance of outer class\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0outer o=<strong>new<\/strong> outer();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/Create an instance of inner class\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 outer.inner3 e=o.<strong>new<\/strong> inner3();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 e.simple();\n\u00a0\u00a0\u00a0\u00a0 }\n}<\/pre>\n<p>Creating an \u2018instance of inner class from outside the outer class instance code\u2019 is similar to creating an \u2018instance of inner class from the outer class\u2019 (which almost sounds like a tongue twister!) ,but since there is no \u201cthis\u201d reference, an instance of outer class is first created and the inner class is then created as follows:\u00a0(Kathy Sierra)<\/p>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Create an instance of outer class\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 outer o=<strong>new<\/strong> outer();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/Create an instance of inner class\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 outer.inner3 e=o.<strong>new<\/strong> inner3();<\/pre>\n<p>In the above example, \u00a0\u2018outer\u2019 is the outer class which has only one private integer variable \u2018i\u2019 and \u2018inner3\u2019 is the inner class which has only one method which prints out the variable \u2018i\u2019. As expected, this is the value that is produced on output:<\/p>\n<p style=\"padding-left: 30px\"><strong>6<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Method-local inner classes:<br \/>\n<\/strong>Having seen \u201cplain\u201d inner classes and the two ways to create instances of it, we next move onto method-local inner class. So far, we have only worked with inner classes being created inside an outer class. What if the inner class is created inside a method? How will we work with such classes?<\/p>\n<pre><strong>package<\/strong> whizlabs;\n<strong>public<\/strong> <strong>class<\/strong> method_local {\n\u00a0\u00a0\u00a0\u00a0 <strong>private<\/strong> <strong>int<\/strong> j=5;\n\u00a0\u00a0\u00a0\u00a0 <strong>void<\/strong> add(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ creation of inner class inside a method\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>class<\/strong> inner{\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>void<\/strong> inner_method(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(j);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Method-local inner class is instantiated\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 inner i=<strong>new<\/strong> inner(); \/\/ Must be instantiated here\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i.inner_method();\n\u00a0\u00a0\u00a0\u00a0 }\u00a0\u00a0\u00a0\u00a0 \n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String[] args) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ <strong>TODO<\/strong> Auto-generated method stub\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 method_local m= <strong>new<\/strong> method_local();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 m.add();\n\u00a0\u00a0\u00a0\u00a0 }\n}<\/pre>\n<p style=\"padding-left: 30px\">Outer class: method_local<br \/>\nOuter class method:\u00a0 add()<br \/>\nOuter class private variable: j<\/p>\n<p style=\"padding-left: 30px\">Method-local inner class: inner<br \/>\nMethod \u2013local inner class method: inner_method()<\/p>\n<p>The code above just prints out the value of the private integer variable \u2018j\u2019. But the important point to note is the creation of the inner class, within the outer class method and instantiating it in the correct place. Here are the key takeaways for the method-local inner classes:<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>Point 1: Method-local inner classes must be instantiated in the method where they are declared.<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Method-local inner classes are like other inner classes in that they can access all the members of the outer class (including private ones) except for one point which is explained below.<\/p>\n<p>&nbsp;<\/p>\n<p>Point 2: <strong><em>Method-local inner classes cannot access the local variables of the method where they are declared except when the local variables are declared \u2018final\u2019. <\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>This is because local variables of a method will not live as long as the inner class itself.<\/p>\n<p>&nbsp;<\/p>\n<p>Having seen method-inner classes and other quirkiness of inner classes, we will move onto anonymous inner classes in the next post.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>Bibliography<\/h4>\n<h4>SCJP 6. In B. B. Kathy Sierra.<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Continuing with our discussion of inner classes, this post will deal with how to create inner classes from outside the outer class instance code and also deal with the next type of inner class (inner classes declared inside methods) Recalling a few crucial points from last post, it should be remembered that An instance of inner class always exists only if an instance of outer class exists. Inner classes never exist alone. Inner classes always have access to all the methods and members of the outer class even if they are declared private. &nbsp; In the previous post, we learnt [&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":[933,967,1148],"class_list":["post-12334","post","type-post","status-publish","format-standard","hentry","category-java","tag-inner-class","tag-java-3","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":0,"uagb_excerpt":"Continuing with our discussion of inner classes, this post will deal with how to create inner classes from outside the outer class instance code and also deal with the next type of inner class (inner classes declared inside methods) Recalling a few crucial points from last post, it should be remembered that An instance of&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12334","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=12334"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12334\/revisions"}],"predecessor-version":[{"id":75992,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12334\/revisions\/75992"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=12334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=12334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=12334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}