Way to call inner class by outer class










2















I know that to instantiate a member inner class, you have two different constructors:



First:



Outer out = new Outer();
Outer.Inner in = out.new Inner();


Second:



Outer.Inner in = new Outer().new Inner();


Now, I don't know why this code compiles:



public class Outer 

private String greeting="Hi";

protected class Inner
public int repeat=3;

public void go()
for (int i =0; i<repeat; i++)
System.out.println(greeting);




public void callInner()
Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
in.go();


public static void main(String args)
Outer out = new Outer();
out.callInner();





Why does it compile?



Thanks a lot!










share|improve this question




























    2















    I know that to instantiate a member inner class, you have two different constructors:



    First:



    Outer out = new Outer();
    Outer.Inner in = out.new Inner();


    Second:



    Outer.Inner in = new Outer().new Inner();


    Now, I don't know why this code compiles:



    public class Outer 

    private String greeting="Hi";

    protected class Inner
    public int repeat=3;

    public void go()
    for (int i =0; i<repeat; i++)
    System.out.println(greeting);




    public void callInner()
    Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
    in.go();


    public static void main(String args)
    Outer out = new Outer();
    out.callInner();





    Why does it compile?



    Thanks a lot!










    share|improve this question


























      2












      2








      2








      I know that to instantiate a member inner class, you have two different constructors:



      First:



      Outer out = new Outer();
      Outer.Inner in = out.new Inner();


      Second:



      Outer.Inner in = new Outer().new Inner();


      Now, I don't know why this code compiles:



      public class Outer 

      private String greeting="Hi";

      protected class Inner
      public int repeat=3;

      public void go()
      for (int i =0; i<repeat; i++)
      System.out.println(greeting);




      public void callInner()
      Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
      in.go();


      public static void main(String args)
      Outer out = new Outer();
      out.callInner();





      Why does it compile?



      Thanks a lot!










      share|improve this question
















      I know that to instantiate a member inner class, you have two different constructors:



      First:



      Outer out = new Outer();
      Outer.Inner in = out.new Inner();


      Second:



      Outer.Inner in = new Outer().new Inner();


      Now, I don't know why this code compiles:



      public class Outer 

      private String greeting="Hi";

      protected class Inner
      public int repeat=3;

      public void go()
      for (int i =0; i<repeat; i++)
      System.out.println(greeting);




      public void callInner()
      Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
      in.go();


      public static void main(String args)
      Outer out = new Outer();
      out.callInner();





      Why does it compile?



      Thanks a lot!







      java constructor inner-classes






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 10:27









      T.J. Crowder

      685k12112191311




      685k12112191311










      asked Nov 13 '18 at 10:25









      Adryr83Adryr83

      849




      849






















          4 Answers
          4






          active

          oldest

          votes


















          6














          As you are instantiating Inner within the scope of Outer (inside an instance method), you do not need to explicitly instantiate referencing the Outer clas, like in your example:



          Outer.Inner in = new Outer().new Inner();


          It is fine to instantiate by just referencing Inner:



           Inner in = new Inner();


          This applies to all instance methods within a class, as long as they are not static.






          share|improve this answer


















          • 1





            Basically it's the equivalent of Inner in = new this.Inner();

            – biziclop
            Nov 13 '18 at 10:37






          • 1





            @biziclop You probably mean Inner in = this.new Inner();

            – LuCio
            Nov 13 '18 at 11:25











          • @LuCio Of course, thanks for the correction.

            – biziclop
            Nov 13 '18 at 17:57


















          2














          First main method will be called



          public static void main(String args)
          Outer out = new Outer();
          out.callInner();



          from here you have create an object of Outer class and called callInner method like below



          public void callInner() 
          Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
          in.go();



          and now you have created an object of Inner and called go method.



           protected class Inner 
          public int repeat=3;

          public void go()
          for (int i =0; i<repeat; i++)
          System.out.println(greeting);





          so it's a simple call all are in a same scope. so need for outer.Inner concept to call.



          It's important to understand that Outer and Inner are related. More specifically, you need an Outer instance in order to create an Inner instance.






          share|improve this answer






























            2














            As your explanation shows, you need an instance of Outer to create an instance of Inner. Since the method callInner is an instance method of Outer (it is not declared static) there is already an instance of Outer present: this



            The code could also be written like that:



            public void callInner() 
            Outer out = this;
            Inner in = out.new Inner();
            in.go();



            Now the code looks similar to your first example.



            But let's keep the code as shown:



            public void callInner() 
            Inner in = new Inner();
            in.go();



            Now if we look under the hood it's basically the same:



             public void callInner();
            Code:
            0: new #21 // class playground/Outer$Inner
            3: dup
            4: aload_0
            5: invokespecial #23 // Method playground/Outer$Inner."<init>":(Lplayground/Outer;)V
            8: astore_1
            9: aload_1
            10: invokevirtual #26 // Method playground/Outer$Inner.go:()V
            13: return


            On line 4 we get aload_0 which loads in instance methods this.




            Compare: Java Tutorials - Inner Class Example






            share|improve this answer
































              1














              When you call the callInner method, you are actually within the scope of Outer class. And the reason, why the compiler accepts calling new Inner() is exactly the same, why you don't have to write explicitly what class your some imagined static variable comes from (when it's the part of the same class you call it). See example below:



              public class Outer 
              private static int x = 1;

              private void innerCall()
              x++;




              In above case, you do exactly the same as in your example with exception, that you use the class and not the variable (which is not really relevant in here). Your syntax would be necessary if wanted to access the class/variable from the outside of the class (scope). It would then look like the thing below:



              public class Outer 
              public static int x = 1;


              Outer.x++;


              Above, you have to explicitly specify what scope you want to access your variable x from. It's just like you wanted to access the file from within the given directory. If you're in this directory, you just access the file by it's name. However, when you are outside of it, you have to write also the directory's name to see the file you want to get.






              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278873%2fway-to-call-inner-class-by-outer-class%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                6














                As you are instantiating Inner within the scope of Outer (inside an instance method), you do not need to explicitly instantiate referencing the Outer clas, like in your example:



                Outer.Inner in = new Outer().new Inner();


                It is fine to instantiate by just referencing Inner:



                 Inner in = new Inner();


                This applies to all instance methods within a class, as long as they are not static.






                share|improve this answer


















                • 1





                  Basically it's the equivalent of Inner in = new this.Inner();

                  – biziclop
                  Nov 13 '18 at 10:37






                • 1





                  @biziclop You probably mean Inner in = this.new Inner();

                  – LuCio
                  Nov 13 '18 at 11:25











                • @LuCio Of course, thanks for the correction.

                  – biziclop
                  Nov 13 '18 at 17:57















                6














                As you are instantiating Inner within the scope of Outer (inside an instance method), you do not need to explicitly instantiate referencing the Outer clas, like in your example:



                Outer.Inner in = new Outer().new Inner();


                It is fine to instantiate by just referencing Inner:



                 Inner in = new Inner();


                This applies to all instance methods within a class, as long as they are not static.






                share|improve this answer


















                • 1





                  Basically it's the equivalent of Inner in = new this.Inner();

                  – biziclop
                  Nov 13 '18 at 10:37






                • 1





                  @biziclop You probably mean Inner in = this.new Inner();

                  – LuCio
                  Nov 13 '18 at 11:25











                • @LuCio Of course, thanks for the correction.

                  – biziclop
                  Nov 13 '18 at 17:57













                6












                6








                6







                As you are instantiating Inner within the scope of Outer (inside an instance method), you do not need to explicitly instantiate referencing the Outer clas, like in your example:



                Outer.Inner in = new Outer().new Inner();


                It is fine to instantiate by just referencing Inner:



                 Inner in = new Inner();


                This applies to all instance methods within a class, as long as they are not static.






                share|improve this answer













                As you are instantiating Inner within the scope of Outer (inside an instance method), you do not need to explicitly instantiate referencing the Outer clas, like in your example:



                Outer.Inner in = new Outer().new Inner();


                It is fine to instantiate by just referencing Inner:



                 Inner in = new Inner();


                This applies to all instance methods within a class, as long as they are not static.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 10:30









                Eamon ScullionEamon Scullion

                682313




                682313







                • 1





                  Basically it's the equivalent of Inner in = new this.Inner();

                  – biziclop
                  Nov 13 '18 at 10:37






                • 1





                  @biziclop You probably mean Inner in = this.new Inner();

                  – LuCio
                  Nov 13 '18 at 11:25











                • @LuCio Of course, thanks for the correction.

                  – biziclop
                  Nov 13 '18 at 17:57












                • 1





                  Basically it's the equivalent of Inner in = new this.Inner();

                  – biziclop
                  Nov 13 '18 at 10:37






                • 1





                  @biziclop You probably mean Inner in = this.new Inner();

                  – LuCio
                  Nov 13 '18 at 11:25











                • @LuCio Of course, thanks for the correction.

                  – biziclop
                  Nov 13 '18 at 17:57







                1




                1





                Basically it's the equivalent of Inner in = new this.Inner();

                – biziclop
                Nov 13 '18 at 10:37





                Basically it's the equivalent of Inner in = new this.Inner();

                – biziclop
                Nov 13 '18 at 10:37




                1




                1





                @biziclop You probably mean Inner in = this.new Inner();

                – LuCio
                Nov 13 '18 at 11:25





                @biziclop You probably mean Inner in = this.new Inner();

                – LuCio
                Nov 13 '18 at 11:25













                @LuCio Of course, thanks for the correction.

                – biziclop
                Nov 13 '18 at 17:57





                @LuCio Of course, thanks for the correction.

                – biziclop
                Nov 13 '18 at 17:57













                2














                First main method will be called



                public static void main(String args)
                Outer out = new Outer();
                out.callInner();



                from here you have create an object of Outer class and called callInner method like below



                public void callInner() 
                Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
                in.go();



                and now you have created an object of Inner and called go method.



                 protected class Inner 
                public int repeat=3;

                public void go()
                for (int i =0; i<repeat; i++)
                System.out.println(greeting);





                so it's a simple call all are in a same scope. so need for outer.Inner concept to call.



                It's important to understand that Outer and Inner are related. More specifically, you need an Outer instance in order to create an Inner instance.






                share|improve this answer



























                  2














                  First main method will be called



                  public static void main(String args)
                  Outer out = new Outer();
                  out.callInner();



                  from here you have create an object of Outer class and called callInner method like below



                  public void callInner() 
                  Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
                  in.go();



                  and now you have created an object of Inner and called go method.



                   protected class Inner 
                  public int repeat=3;

                  public void go()
                  for (int i =0; i<repeat; i++)
                  System.out.println(greeting);





                  so it's a simple call all are in a same scope. so need for outer.Inner concept to call.



                  It's important to understand that Outer and Inner are related. More specifically, you need an Outer instance in order to create an Inner instance.






                  share|improve this answer

























                    2












                    2








                    2







                    First main method will be called



                    public static void main(String args)
                    Outer out = new Outer();
                    out.callInner();



                    from here you have create an object of Outer class and called callInner method like below



                    public void callInner() 
                    Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
                    in.go();



                    and now you have created an object of Inner and called go method.



                     protected class Inner 
                    public int repeat=3;

                    public void go()
                    for (int i =0; i<repeat; i++)
                    System.out.println(greeting);





                    so it's a simple call all are in a same scope. so need for outer.Inner concept to call.



                    It's important to understand that Outer and Inner are related. More specifically, you need an Outer instance in order to create an Inner instance.






                    share|improve this answer













                    First main method will be called



                    public static void main(String args)
                    Outer out = new Outer();
                    out.callInner();



                    from here you have create an object of Outer class and called callInner method like below



                    public void callInner() 
                    Inner in = new Inner(); //in my opinion the correct constructor is Outer.Inner in = new Inner()
                    in.go();



                    and now you have created an object of Inner and called go method.



                     protected class Inner 
                    public int repeat=3;

                    public void go()
                    for (int i =0; i<repeat; i++)
                    System.out.println(greeting);





                    so it's a simple call all are in a same scope. so need for outer.Inner concept to call.



                    It's important to understand that Outer and Inner are related. More specifically, you need an Outer instance in order to create an Inner instance.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 10:34









                    GauravRai1512GauravRai1512

                    58811




                    58811





















                        2














                        As your explanation shows, you need an instance of Outer to create an instance of Inner. Since the method callInner is an instance method of Outer (it is not declared static) there is already an instance of Outer present: this



                        The code could also be written like that:



                        public void callInner() 
                        Outer out = this;
                        Inner in = out.new Inner();
                        in.go();



                        Now the code looks similar to your first example.



                        But let's keep the code as shown:



                        public void callInner() 
                        Inner in = new Inner();
                        in.go();



                        Now if we look under the hood it's basically the same:



                         public void callInner();
                        Code:
                        0: new #21 // class playground/Outer$Inner
                        3: dup
                        4: aload_0
                        5: invokespecial #23 // Method playground/Outer$Inner."<init>":(Lplayground/Outer;)V
                        8: astore_1
                        9: aload_1
                        10: invokevirtual #26 // Method playground/Outer$Inner.go:()V
                        13: return


                        On line 4 we get aload_0 which loads in instance methods this.




                        Compare: Java Tutorials - Inner Class Example






                        share|improve this answer





























                          2














                          As your explanation shows, you need an instance of Outer to create an instance of Inner. Since the method callInner is an instance method of Outer (it is not declared static) there is already an instance of Outer present: this



                          The code could also be written like that:



                          public void callInner() 
                          Outer out = this;
                          Inner in = out.new Inner();
                          in.go();



                          Now the code looks similar to your first example.



                          But let's keep the code as shown:



                          public void callInner() 
                          Inner in = new Inner();
                          in.go();



                          Now if we look under the hood it's basically the same:



                           public void callInner();
                          Code:
                          0: new #21 // class playground/Outer$Inner
                          3: dup
                          4: aload_0
                          5: invokespecial #23 // Method playground/Outer$Inner."<init>":(Lplayground/Outer;)V
                          8: astore_1
                          9: aload_1
                          10: invokevirtual #26 // Method playground/Outer$Inner.go:()V
                          13: return


                          On line 4 we get aload_0 which loads in instance methods this.




                          Compare: Java Tutorials - Inner Class Example






                          share|improve this answer



























                            2












                            2








                            2







                            As your explanation shows, you need an instance of Outer to create an instance of Inner. Since the method callInner is an instance method of Outer (it is not declared static) there is already an instance of Outer present: this



                            The code could also be written like that:



                            public void callInner() 
                            Outer out = this;
                            Inner in = out.new Inner();
                            in.go();



                            Now the code looks similar to your first example.



                            But let's keep the code as shown:



                            public void callInner() 
                            Inner in = new Inner();
                            in.go();



                            Now if we look under the hood it's basically the same:



                             public void callInner();
                            Code:
                            0: new #21 // class playground/Outer$Inner
                            3: dup
                            4: aload_0
                            5: invokespecial #23 // Method playground/Outer$Inner."<init>":(Lplayground/Outer;)V
                            8: astore_1
                            9: aload_1
                            10: invokevirtual #26 // Method playground/Outer$Inner.go:()V
                            13: return


                            On line 4 we get aload_0 which loads in instance methods this.




                            Compare: Java Tutorials - Inner Class Example






                            share|improve this answer















                            As your explanation shows, you need an instance of Outer to create an instance of Inner. Since the method callInner is an instance method of Outer (it is not declared static) there is already an instance of Outer present: this



                            The code could also be written like that:



                            public void callInner() 
                            Outer out = this;
                            Inner in = out.new Inner();
                            in.go();



                            Now the code looks similar to your first example.



                            But let's keep the code as shown:



                            public void callInner() 
                            Inner in = new Inner();
                            in.go();



                            Now if we look under the hood it's basically the same:



                             public void callInner();
                            Code:
                            0: new #21 // class playground/Outer$Inner
                            3: dup
                            4: aload_0
                            5: invokespecial #23 // Method playground/Outer$Inner."<init>":(Lplayground/Outer;)V
                            8: astore_1
                            9: aload_1
                            10: invokevirtual #26 // Method playground/Outer$Inner.go:()V
                            13: return


                            On line 4 we get aload_0 which loads in instance methods this.




                            Compare: Java Tutorials - Inner Class Example







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '18 at 11:09

























                            answered Nov 13 '18 at 10:35









                            LuCioLuCio

                            2,8041823




                            2,8041823





















                                1














                                When you call the callInner method, you are actually within the scope of Outer class. And the reason, why the compiler accepts calling new Inner() is exactly the same, why you don't have to write explicitly what class your some imagined static variable comes from (when it's the part of the same class you call it). See example below:



                                public class Outer 
                                private static int x = 1;

                                private void innerCall()
                                x++;




                                In above case, you do exactly the same as in your example with exception, that you use the class and not the variable (which is not really relevant in here). Your syntax would be necessary if wanted to access the class/variable from the outside of the class (scope). It would then look like the thing below:



                                public class Outer 
                                public static int x = 1;


                                Outer.x++;


                                Above, you have to explicitly specify what scope you want to access your variable x from. It's just like you wanted to access the file from within the given directory. If you're in this directory, you just access the file by it's name. However, when you are outside of it, you have to write also the directory's name to see the file you want to get.






                                share|improve this answer





























                                  1














                                  When you call the callInner method, you are actually within the scope of Outer class. And the reason, why the compiler accepts calling new Inner() is exactly the same, why you don't have to write explicitly what class your some imagined static variable comes from (when it's the part of the same class you call it). See example below:



                                  public class Outer 
                                  private static int x = 1;

                                  private void innerCall()
                                  x++;




                                  In above case, you do exactly the same as in your example with exception, that you use the class and not the variable (which is not really relevant in here). Your syntax would be necessary if wanted to access the class/variable from the outside of the class (scope). It would then look like the thing below:



                                  public class Outer 
                                  public static int x = 1;


                                  Outer.x++;


                                  Above, you have to explicitly specify what scope you want to access your variable x from. It's just like you wanted to access the file from within the given directory. If you're in this directory, you just access the file by it's name. However, when you are outside of it, you have to write also the directory's name to see the file you want to get.






                                  share|improve this answer



























                                    1












                                    1








                                    1







                                    When you call the callInner method, you are actually within the scope of Outer class. And the reason, why the compiler accepts calling new Inner() is exactly the same, why you don't have to write explicitly what class your some imagined static variable comes from (when it's the part of the same class you call it). See example below:



                                    public class Outer 
                                    private static int x = 1;

                                    private void innerCall()
                                    x++;




                                    In above case, you do exactly the same as in your example with exception, that you use the class and not the variable (which is not really relevant in here). Your syntax would be necessary if wanted to access the class/variable from the outside of the class (scope). It would then look like the thing below:



                                    public class Outer 
                                    public static int x = 1;


                                    Outer.x++;


                                    Above, you have to explicitly specify what scope you want to access your variable x from. It's just like you wanted to access the file from within the given directory. If you're in this directory, you just access the file by it's name. However, when you are outside of it, you have to write also the directory's name to see the file you want to get.






                                    share|improve this answer















                                    When you call the callInner method, you are actually within the scope of Outer class. And the reason, why the compiler accepts calling new Inner() is exactly the same, why you don't have to write explicitly what class your some imagined static variable comes from (when it's the part of the same class you call it). See example below:



                                    public class Outer 
                                    private static int x = 1;

                                    private void innerCall()
                                    x++;




                                    In above case, you do exactly the same as in your example with exception, that you use the class and not the variable (which is not really relevant in here). Your syntax would be necessary if wanted to access the class/variable from the outside of the class (scope). It would then look like the thing below:



                                    public class Outer 
                                    public static int x = 1;


                                    Outer.x++;


                                    Above, you have to explicitly specify what scope you want to access your variable x from. It's just like you wanted to access the file from within the given directory. If you're in this directory, you just access the file by it's name. However, when you are outside of it, you have to write also the directory's name to see the file you want to get.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 13 '18 at 11:00









                                    Eamon Scullion

                                    682313




                                    682313










                                    answered Nov 13 '18 at 10:40









                                    Adrian SzymańskiAdrian Szymański

                                    112




                                    112



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278873%2fway-to-call-inner-class-by-outer-class%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Use pre created SQLite database for Android project in kotlin

                                        Darth Vader #20

                                        Ondo