Why interface can only be declared in top-level class?
up vote
10
down vote
favorite
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
add a comment |
up vote
10
down vote
favorite
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
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
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
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
java oop class interface inner-classes
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
add a comment |
add a comment |
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.
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
add a comment |
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
This answer will make Thomas's answer clearer. +1.
– Luke Vo
Aug 23 '11 at 16:02
add a comment |
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.
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
add a comment |
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.
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 thempublic
.
– skaffman
Aug 23 '11 at 15:47
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
This answer will make Thomas's answer clearer. +1.
– Luke Vo
Aug 23 '11 at 16:02
add a comment |
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
This answer will make Thomas's answer clearer. +1.
– Luke Vo
Aug 23 '11 at 16:02
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
By definition a top level class and its inner class(es) are tightly coupled. Interfaces are a means of reducing coupling.
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
add a comment |
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
add a comment |
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.
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 thempublic
.
– skaffman
Aug 23 '11 at 15:47
add a comment |
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.
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 thempublic
.
– skaffman
Aug 23 '11 at 15:47
add a comment |
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.
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.
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 thempublic
.
– skaffman
Aug 23 '11 at 15:47
add a comment |
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 thempublic
.
– 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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