{"id":289,"date":"2009-09-23T06:24:58","date_gmt":"2009-09-23T06:24:58","guid":{"rendered":"http:\/\/kubasek.com\/blog\/pragmatic_craftsman\/?p=289"},"modified":"2009-09-23T06:24:58","modified_gmt":"2009-09-23T06:24:58","slug":"java-inner-classes-part-3-nested-classes","status":"publish","type":"post","link":"https:\/\/pragmaticcraftsman.kubasek.com\/?p=289","title":{"rendered":"Java Inner Classes &#8211; Part 3 &#8211; Nested Classes"},"content":{"rendered":"<p>In <a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_intro_1.php\">Part 1<\/a>, I&#8217;ve covered the basics, in <a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_part_2.php\">part 2<\/a>, anonymous inner classes. Is there anything left about inner classes? Yes, there is. I warned you that inner classes are a beast. \ud83d\ude42<\/p>\n<p><p>In this part, I&#8217;ll cover nested classes.<\/p>\n<p><p>When you create an inner class, there is a connection between the enclosed class and the inner class. Because of that, the inner class can access and manipulate the enclosed class. <\/p>\n<p><p>Take that connection away and you have a nested class. A nested class is a static inner class. With a nested class, 1) you don&#8217;t need an outer class to create the instance and 2) you can&#8217;t access non-static outer-class object from the nested instance. Below is an example.<\/p>\n<p>\n<pre class=\"brush:java\">\npublic class StaticInner {\n  String s = \"test\";\n\n  public static class Inner {\n    private int counter = 1;\n    public int increment() {\n       return counter++;\n    }\n\n    public void printS() {\n      \/\/ StaticInner.this.s;\n      \/\/ ERROR: No enclosing instance of the type StaticInner is\n      \/\/ accessible in scope\n    }\n  }\n\npublic static class Inner2 {\n  private static int counter = 1;\n  public static int increment() {\n    return counter++;\n  }\n}\n\npublic static class Inner3 {\n  public static class Inner3Inner {\n    public void sayHi() {\n      System.out.println(\"Hi\");\n    }\n  }\n}\n\npublic static void main(String[] args) {\n  System.out.println(\"Inner\");\n  Inner inner = new Inner();\n  System.out.println(inner.increment());  \/\/ \"1\"\n  System.out.println(inner.increment()); \"2\"\n  Inner inner2 = new Inner();\n  System.out.println(inner2.increment()); \/\/ \"1\"\n\n  \/\/ Inner2 has a static method\n  System.out.println(\"Inner2\");\n  System.out.println(Inner2.increment()); \/\/ \"1\"\n  System.out.println(Inner2.increment()); \/\/ \"2\"\n  \/\/ Inner3 has an inner class\n  System.out.println(\"Inner3Inner\");\n  Inner3Inner subInner = new Inner3Inner();\n  subInner.sayHi(); \/\/ \"Hi\"\n  }\n}<\/pre>\n<\/p>\n<p><p>In the main method, you can see that you can just instantiate Inner with new Inner(). Also in Inner, printS method tries to access StaticInner variable but it&#8217;s not allowed to. There is no connection between the two classes.<\/p>\n<p><p>Initially, I thought a &#8220;static class&#8221; can have only one instance. That&#8217;s not true. Inner can have multiple instances, inner2 is a seperate instance and has its own object.<\/p>\n<p><p>Inner2 is an example of a static instance. You can refer to in directly by Inner2.increment().<\/p>\n<p><p>Inner2 shows that static inners can have more than one layer, Inner3Inner is defined inside Inner3.<\/p>\n<p><h2>Classes inside interfaces<\/h2>\n<p>Yes, you read that correctly. You can have an inner class within an interface! I did not know this was possible. Not that I would want to use it, but still&#8230; Take a look at the following example.<\/p>\n<p>\n<pre class=\"brush:java\">\n\/\/ Thinking in Java example\npublic interface ClassInInterface {\n  void howdy();\n\n  class Test implements ClassInInterface {\n    public void howdy() {\n      System.out.println(\"Howdy!\");\n    }\n  }\n}\n\n\/\/ Usage\nClassInInterface test = new Test();\ntest.howdy();\n\n\/* Output: Howdy!<p>*\/\/\/ :~<\/pre>\n<\/p>\n<p><p>It&#8217;s completely valid! Why would you want to do it that way? One valid reason is that if you wanted to have a common implementation of the interface. The downside is that the interface is &#8220;heavier&#8221; to carry around, but that&#8217;s probably very minor. <\/p>\n<p><h2>Multi nested inner classes<\/h2>\n<p>Back to the basic inner classes. Does a 2nd or 3rd level inner class have access to the layers above? Yes, it does, it has access to all layers. Thinking in Java has a great example, see below.<\/p>\n<p>\n<pre class=\"brush:java\">\n\/\/ Nested classes can access all members of all\n\/\/ levels of the classes they are nested within.\nclass MNA {\n  private void f() { }\n  class A {\n    private void g() { }\n    public class B {\n      void h() {\n        g();\n        f();\n      }\n    }\n  }\n}\n\npublic class MultiNestingAccess {\n  public static void main(String[] args) {\n    MNA mna = new MNA();\n    MNA.A mnaa = mna.new A();\n    MNA.A.B mnaab = mnaa.new B();\n    mnaab.h();\n  }\n} \/\/ \/:~<\/pre>\n<\/p>\n<p><h3>More to come&#8230;<\/h3>\n<p>I don&#8217;t like long tutorials nor long chapters. That&#8217;s why I&#8217;m breaking these into parts. When will it end? I have done 3 parts already and I&#8217;m not done! In Part 4, I&#8217;ll try to cover another advanced topic, Multiple Inheritance. Bruce Eckel has an interesting example.<\/p>\n<p><p><strong>Reference<\/strong><br \/><?php book(\"Thinking in Java (4th)\", \"0131872486\") ?>, Bruce Eckel<br \/><a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_intro_1.php\">Java Inner Classes &#8211; Part 1 &#8211; Intro<\/a><br \/><a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_part_2.php\">Java Inner Classes &#8211; Part 2 &#8211; Anonymous<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 1, I&#8217;ve covered the basics, in part 2, anonymous inner classes. Is there anything left about inner classes? Yes, there is. I warned you that inner classes are a beast. \ud83d\ude42 In this part, I&#8217;ll cover nested classes. When you create an inner class, there is a connection between the enclosed class and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[17,26,56],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-java","tag-advanced","tag-innerclass","tag-java"],"_links":{"self":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289"}],"version-history":[{"count":0,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions"}],"wp:attachment":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}