Why interface can only be declared in top-level class?









up vote
10
down vote

favorite
5












Alright, I know it's the rule:




According to JLS: 8.1.3 Inner Classes and Enclosing Instances, inner
classes may not declare static initializers or member interfaces.
Inner classes may not declare static members, unless they are
compile-time constant fields.



According to 8.5.2 Static Member Type Declarations, "Member interfaces
are always implicitly static. It is permitted but not required for the
declaration of a member interface to explicitly list the static
modifier". They are always top-level, not inner.




I just wonder why. What may happen if we are allowed to declare interface within an inner class? Won't inner class become top-level class if I put it into another Class file?










share|improve this question



























    up vote
    10
    down vote

    favorite
    5












    Alright, I know it's the rule:




    According to JLS: 8.1.3 Inner Classes and Enclosing Instances, inner
    classes may not declare static initializers or member interfaces.
    Inner classes may not declare static members, unless they are
    compile-time constant fields.



    According to 8.5.2 Static Member Type Declarations, "Member interfaces
    are always implicitly static. It is permitted but not required for the
    declaration of a member interface to explicitly list the static
    modifier". They are always top-level, not inner.




    I just wonder why. What may happen if we are allowed to declare interface within an inner class? Won't inner class become top-level class if I put it into another Class file?










    share|improve this question

























      up vote
      10
      down vote

      favorite
      5









      up vote
      10
      down vote

      favorite
      5






      5





      Alright, I know it's the rule:




      According to JLS: 8.1.3 Inner Classes and Enclosing Instances, inner
      classes may not declare static initializers or member interfaces.
      Inner classes may not declare static members, unless they are
      compile-time constant fields.



      According to 8.5.2 Static Member Type Declarations, "Member interfaces
      are always implicitly static. It is permitted but not required for the
      declaration of a member interface to explicitly list the static
      modifier". They are always top-level, not inner.




      I just wonder why. What may happen if we are allowed to declare interface within an inner class? Won't inner class become top-level class if I put it into another Class file?










      share|improve this question















      Alright, I know it's the rule:




      According to JLS: 8.1.3 Inner Classes and Enclosing Instances, inner
      classes may not declare static initializers or member interfaces.
      Inner classes may not declare static members, unless they are
      compile-time constant fields.



      According to 8.5.2 Static Member Type Declarations, "Member interfaces
      are always implicitly static. It is permitted but not required for the
      declaration of a member interface to explicitly list the static
      modifier". They are always top-level, not inner.




      I just wonder why. What may happen if we are allowed to declare interface within an inner class? Won't inner class become top-level class if I put it into another Class file?







      java oop class interface inner-classes






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 3 '11 at 3:31









      animuson

      41.9k22113129




      41.9k22113129










      asked Aug 23 '11 at 15:43









      Luke Vo

      4,9741656107




      4,9741656107






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted











          Won't inner class become top-level class if I put it into another Class file?




          No, it still is an inner class, which the filename indicates (IIRC it's OuterClass$InnerClass.class).



          Inner classes have access to the outer class' attributes, i.e. they depend on their outer class' instance. With interfaces you couldn't do this. Think of a completely unrelated class that would have to be created by the corresponding outer class' instance. How would that be done if the outer class doesn't know who implements that interface?



          What you can do is declare static interfaces in your outer class, thus merely using the outer as a namespace:



          public class OuterClass 
          public static interface InnerInterface //protected and private would be fine too, depending on what makes sense




          Edit: actually, I misread the question and since interfaces are static anyways, here's an updated code snippet:



           public class OuterClass 
          public static InnerClass //static inner class making OuterClass just be a namespace
          public interface InnerInnerInterface //protected and private would be fine too, depending on what makes sense





          As a workaround you could define an abstract inner inner class, with the drawback that you have to stick to the single inheritance constraint.






          share|improve this answer






















          • Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
            – Luke Vo
            Aug 23 '11 at 15:53







          • 2




            @W.N.: Interfaces are implicitly static. That declaration is just redudant.
            – Ryan Stewart
            Aug 23 '11 at 15:57







          • 1




            @Ryan good point, I also just reread the question and will update my answer.
            – Thomas
            Aug 24 '11 at 8:55

















          up vote
          4
          down vote













          Think of it in terms of static vs. non-static context. A "top-level" class establishes a static context because it can be accessed without any enclosing instance. I.e. you can access top-level classes from a main method. The same applies to any static members of a top-level class. An inner class, however, neither exists in* nor establishes any static context. Therefore it can't have any static members, and it can only be accessed via an instance of its containing class, like constructors and other instance members. From a main method, you wouldn't be able to say Outer.Inner.SOME_FIELD because members of an inner class only have meaning with respect to the containing class.



          *sort of






          share|improve this answer






















          • This answer will make Thomas's answer clearer. +1.
            – Luke Vo
            Aug 23 '11 at 16:02

















          up vote
          2
          down vote













          By definition a top level class and its inner class(es) are tightly coupled. Interfaces are a means of reducing coupling.






          share|improve this answer
















          • 1




            Even if I only need to privately use that interface for the inner class?
            – Luke Vo
            Aug 23 '11 at 15:55










          • That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
            – Ryan Stewart
            Aug 23 '11 at 16:08


















          up vote
          1
          down vote













          Inner classes are supposed to be implementation details of the top-level class and should therefore be invisible to the client. Any functionality you wish to access of an inner class should be done so through the top-level class, because conceptually speaking, that functionality should be visible only as functionality of the top-level class, so that the class designer can swap out or otherwise drastically change inner classes without breaking clients' builds.






          share|improve this answer
















          • 2




            That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
            – skaffman
            Aug 23 '11 at 15:47










          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',
          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%2f7163731%2fwhy-interface-can-only-be-declared-in-top-level-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








          up vote
          7
          down vote



          accepted











          Won't inner class become top-level class if I put it into another Class file?




          No, it still is an inner class, which the filename indicates (IIRC it's OuterClass$InnerClass.class).



          Inner classes have access to the outer class' attributes, i.e. they depend on their outer class' instance. With interfaces you couldn't do this. Think of a completely unrelated class that would have to be created by the corresponding outer class' instance. How would that be done if the outer class doesn't know who implements that interface?



          What you can do is declare static interfaces in your outer class, thus merely using the outer as a namespace:



          public class OuterClass 
          public static interface InnerInterface //protected and private would be fine too, depending on what makes sense




          Edit: actually, I misread the question and since interfaces are static anyways, here's an updated code snippet:



           public class OuterClass 
          public static InnerClass //static inner class making OuterClass just be a namespace
          public interface InnerInnerInterface //protected and private would be fine too, depending on what makes sense





          As a workaround you could define an abstract inner inner class, with the drawback that you have to stick to the single inheritance constraint.






          share|improve this answer






















          • Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
            – Luke Vo
            Aug 23 '11 at 15:53







          • 2




            @W.N.: Interfaces are implicitly static. That declaration is just redudant.
            – Ryan Stewart
            Aug 23 '11 at 15:57







          • 1




            @Ryan good point, I also just reread the question and will update my answer.
            – Thomas
            Aug 24 '11 at 8:55














          up vote
          7
          down vote



          accepted











          Won't inner class become top-level class if I put it into another Class file?




          No, it still is an inner class, which the filename indicates (IIRC it's OuterClass$InnerClass.class).



          Inner classes have access to the outer class' attributes, i.e. they depend on their outer class' instance. With interfaces you couldn't do this. Think of a completely unrelated class that would have to be created by the corresponding outer class' instance. How would that be done if the outer class doesn't know who implements that interface?



          What you can do is declare static interfaces in your outer class, thus merely using the outer as a namespace:



          public class OuterClass 
          public static interface InnerInterface //protected and private would be fine too, depending on what makes sense




          Edit: actually, I misread the question and since interfaces are static anyways, here's an updated code snippet:



           public class OuterClass 
          public static InnerClass //static inner class making OuterClass just be a namespace
          public interface InnerInnerInterface //protected and private would be fine too, depending on what makes sense





          As a workaround you could define an abstract inner inner class, with the drawback that you have to stick to the single inheritance constraint.






          share|improve this answer






















          • Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
            – Luke Vo
            Aug 23 '11 at 15:53







          • 2




            @W.N.: Interfaces are implicitly static. That declaration is just redudant.
            – Ryan Stewart
            Aug 23 '11 at 15:57







          • 1




            @Ryan good point, I also just reread the question and will update my answer.
            – Thomas
            Aug 24 '11 at 8:55












          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted







          Won't inner class become top-level class if I put it into another Class file?




          No, it still is an inner class, which the filename indicates (IIRC it's OuterClass$InnerClass.class).



          Inner classes have access to the outer class' attributes, i.e. they depend on their outer class' instance. With interfaces you couldn't do this. Think of a completely unrelated class that would have to be created by the corresponding outer class' instance. How would that be done if the outer class doesn't know who implements that interface?



          What you can do is declare static interfaces in your outer class, thus merely using the outer as a namespace:



          public class OuterClass 
          public static interface InnerInterface //protected and private would be fine too, depending on what makes sense




          Edit: actually, I misread the question and since interfaces are static anyways, here's an updated code snippet:



           public class OuterClass 
          public static InnerClass //static inner class making OuterClass just be a namespace
          public interface InnerInnerInterface //protected and private would be fine too, depending on what makes sense





          As a workaround you could define an abstract inner inner class, with the drawback that you have to stick to the single inheritance constraint.






          share|improve this answer















          Won't inner class become top-level class if I put it into another Class file?




          No, it still is an inner class, which the filename indicates (IIRC it's OuterClass$InnerClass.class).



          Inner classes have access to the outer class' attributes, i.e. they depend on their outer class' instance. With interfaces you couldn't do this. Think of a completely unrelated class that would have to be created by the corresponding outer class' instance. How would that be done if the outer class doesn't know who implements that interface?



          What you can do is declare static interfaces in your outer class, thus merely using the outer as a namespace:



          public class OuterClass 
          public static interface InnerInterface //protected and private would be fine too, depending on what makes sense




          Edit: actually, I misread the question and since interfaces are static anyways, here's an updated code snippet:



           public class OuterClass 
          public static InnerClass //static inner class making OuterClass just be a namespace
          public interface InnerInnerInterface //protected and private would be fine too, depending on what makes sense





          As a workaround you could define an abstract inner inner class, with the drawback that you have to stick to the single inheritance constraint.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 24 '11 at 9:01

























          answered Aug 23 '11 at 15:47









          Thomas

          68.6k989123




          68.6k989123











          • Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
            – Luke Vo
            Aug 23 '11 at 15:53







          • 2




            @W.N.: Interfaces are implicitly static. That declaration is just redudant.
            – Ryan Stewart
            Aug 23 '11 at 15:57







          • 1




            @Ryan good point, I also just reread the question and will update my answer.
            – Thomas
            Aug 24 '11 at 8:55
















          • Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
            – Luke Vo
            Aug 23 '11 at 15:53







          • 2




            @W.N.: Interfaces are implicitly static. That declaration is just redudant.
            – Ryan Stewart
            Aug 23 '11 at 15:57







          • 1




            @Ryan good point, I also just reread the question and will update my answer.
            – Thomas
            Aug 24 '11 at 8:55















          Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
          – Luke Vo
          Aug 23 '11 at 15:53





          Hmm, interesting! I never know that interface can be declared as static. What does "static" mean here? I've tried Googled static interface but haven't found out anything. P/s: Just edit my post at the line you quote, to correct grammar error.
          – Luke Vo
          Aug 23 '11 at 15:53





          2




          2




          @W.N.: Interfaces are implicitly static. That declaration is just redudant.
          – Ryan Stewart
          Aug 23 '11 at 15:57





          @W.N.: Interfaces are implicitly static. That declaration is just redudant.
          – Ryan Stewart
          Aug 23 '11 at 15:57





          1




          1




          @Ryan good point, I also just reread the question and will update my answer.
          – Thomas
          Aug 24 '11 at 8:55




          @Ryan good point, I also just reread the question and will update my answer.
          – Thomas
          Aug 24 '11 at 8:55












          up vote
          4
          down vote













          Think of it in terms of static vs. non-static context. A "top-level" class establishes a static context because it can be accessed without any enclosing instance. I.e. you can access top-level classes from a main method. The same applies to any static members of a top-level class. An inner class, however, neither exists in* nor establishes any static context. Therefore it can't have any static members, and it can only be accessed via an instance of its containing class, like constructors and other instance members. From a main method, you wouldn't be able to say Outer.Inner.SOME_FIELD because members of an inner class only have meaning with respect to the containing class.



          *sort of






          share|improve this answer






















          • This answer will make Thomas's answer clearer. +1.
            – Luke Vo
            Aug 23 '11 at 16:02














          up vote
          4
          down vote













          Think of it in terms of static vs. non-static context. A "top-level" class establishes a static context because it can be accessed without any enclosing instance. I.e. you can access top-level classes from a main method. The same applies to any static members of a top-level class. An inner class, however, neither exists in* nor establishes any static context. Therefore it can't have any static members, and it can only be accessed via an instance of its containing class, like constructors and other instance members. From a main method, you wouldn't be able to say Outer.Inner.SOME_FIELD because members of an inner class only have meaning with respect to the containing class.



          *sort of






          share|improve this answer






















          • This answer will make Thomas's answer clearer. +1.
            – Luke Vo
            Aug 23 '11 at 16:02












          up vote
          4
          down vote










          up vote
          4
          down vote









          Think of it in terms of static vs. non-static context. A "top-level" class establishes a static context because it can be accessed without any enclosing instance. I.e. you can access top-level classes from a main method. The same applies to any static members of a top-level class. An inner class, however, neither exists in* nor establishes any static context. Therefore it can't have any static members, and it can only be accessed via an instance of its containing class, like constructors and other instance members. From a main method, you wouldn't be able to say Outer.Inner.SOME_FIELD because members of an inner class only have meaning with respect to the containing class.



          *sort of






          share|improve this answer














          Think of it in terms of static vs. non-static context. A "top-level" class establishes a static context because it can be accessed without any enclosing instance. I.e. you can access top-level classes from a main method. The same applies to any static members of a top-level class. An inner class, however, neither exists in* nor establishes any static context. Therefore it can't have any static members, and it can only be accessed via an instance of its containing class, like constructors and other instance members. From a main method, you wouldn't be able to say Outer.Inner.SOME_FIELD because members of an inner class only have meaning with respect to the containing class.



          *sort of







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 23 '11 at 16:10

























          answered Aug 23 '11 at 15:56









          Ryan Stewart

          96.2k16147180




          96.2k16147180











          • This answer will make Thomas's answer clearer. +1.
            – Luke Vo
            Aug 23 '11 at 16:02
















          • This answer will make Thomas's answer clearer. +1.
            – Luke Vo
            Aug 23 '11 at 16:02















          This answer will make Thomas's answer clearer. +1.
          – Luke Vo
          Aug 23 '11 at 16:02




          This answer will make Thomas's answer clearer. +1.
          – Luke Vo
          Aug 23 '11 at 16:02










          up vote
          2
          down vote













          By definition a top level class and its inner class(es) are tightly coupled. Interfaces are a means of reducing coupling.






          share|improve this answer
















          • 1




            Even if I only need to privately use that interface for the inner class?
            – Luke Vo
            Aug 23 '11 at 15:55










          • That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
            – Ryan Stewart
            Aug 23 '11 at 16:08















          up vote
          2
          down vote













          By definition a top level class and its inner class(es) are tightly coupled. Interfaces are a means of reducing coupling.






          share|improve this answer
















          • 1




            Even if I only need to privately use that interface for the inner class?
            – Luke Vo
            Aug 23 '11 at 15:55










          • That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
            – Ryan Stewart
            Aug 23 '11 at 16:08













          up vote
          2
          down vote










          up vote
          2
          down vote









          By definition a top level class and its inner class(es) are tightly coupled. Interfaces are a means of reducing coupling.






          share|improve this answer












          By definition a top level class and its inner class(es) are tightly coupled. Interfaces are a means of reducing coupling.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 23 '11 at 15:49









          John Topley

          84.2k41180228




          84.2k41180228







          • 1




            Even if I only need to privately use that interface for the inner class?
            – Luke Vo
            Aug 23 '11 at 15:55










          • That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
            – Ryan Stewart
            Aug 23 '11 at 16:08













          • 1




            Even if I only need to privately use that interface for the inner class?
            – Luke Vo
            Aug 23 '11 at 15:55










          • That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
            – Ryan Stewart
            Aug 23 '11 at 16:08








          1




          1




          Even if I only need to privately use that interface for the inner class?
          – Luke Vo
          Aug 23 '11 at 15:55




          Even if I only need to privately use that interface for the inner class?
          – Luke Vo
          Aug 23 '11 at 15:55












          That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
          – Ryan Stewart
          Aug 23 '11 at 16:08





          That's not necessarily a good argument, since I've written a number of private, nested interfaces that are used only within the scope of a single class.
          – Ryan Stewart
          Aug 23 '11 at 16:08











          up vote
          1
          down vote













          Inner classes are supposed to be implementation details of the top-level class and should therefore be invisible to the client. Any functionality you wish to access of an inner class should be done so through the top-level class, because conceptually speaking, that functionality should be visible only as functionality of the top-level class, so that the class designer can swap out or otherwise drastically change inner classes without breaking clients' builds.






          share|improve this answer
















          • 2




            That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
            – skaffman
            Aug 23 '11 at 15:47














          up vote
          1
          down vote













          Inner classes are supposed to be implementation details of the top-level class and should therefore be invisible to the client. Any functionality you wish to access of an inner class should be done so through the top-level class, because conceptually speaking, that functionality should be visible only as functionality of the top-level class, so that the class designer can swap out or otherwise drastically change inner classes without breaking clients' builds.






          share|improve this answer
















          • 2




            That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
            – skaffman
            Aug 23 '11 at 15:47












          up vote
          1
          down vote










          up vote
          1
          down vote









          Inner classes are supposed to be implementation details of the top-level class and should therefore be invisible to the client. Any functionality you wish to access of an inner class should be done so through the top-level class, because conceptually speaking, that functionality should be visible only as functionality of the top-level class, so that the class designer can swap out or otherwise drastically change inner classes without breaking clients' builds.






          share|improve this answer












          Inner classes are supposed to be implementation details of the top-level class and should therefore be invisible to the client. Any functionality you wish to access of an inner class should be done so through the top-level class, because conceptually speaking, that functionality should be visible only as functionality of the top-level class, so that the class designer can swap out or otherwise drastically change inner classes without breaking clients' builds.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 23 '11 at 15:47









          matthias

          1,50311227




          1,50311227







          • 2




            That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
            – skaffman
            Aug 23 '11 at 15:47












          • 2




            That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
            – skaffman
            Aug 23 '11 at 15:47







          2




          2




          That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
          – skaffman
          Aug 23 '11 at 15:47




          That's all a matter of opinion. If inner classes were only for internal use of the parent class, you wouldn't be allowed to make them public.
          – skaffman
          Aug 23 '11 at 15:47

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7163731%2fwhy-interface-can-only-be-declared-in-top-level-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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto