• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface Declarations - default static?

 
Rodger Donaphon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was preparing for the SCJP and discovered something about interface declarations not in my Liang textbook (unless I missed it).

The following code compiles OK.



Originally, I had the interfaces declared static, but when I took that away it still compiles.

It looks like I don't need a concrete class to use an interface declared in an abstract class.

If you try the above with inside classes instead of inside interfaces, you have to declare the inside classes static.

Is it true that in Java, interface declarations are static? Or is there another explanation?

Thanks in advance for any comments. I want to make sure I know the rules.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member interfaces (interfaces declared within another class or interface) are implicitly static. Top-level interfaces are not (since the word doesn't really mean anything in that context.)
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that in Java, interface declarations are static? No

See here for more info on nested types: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodger Donaphon wrote:Is it true that in Java, interface declarations are static? Or is there another explanation?

Don't think so. To be honest, I'd never thought about it, but it does actually make sense: After all, you can't instantiate an interface, so what would an "inner" interface do?.

Winston
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, that seems to be the same question I just answered. My original answer still applies.

From JLS7 8.5.1:
A member interface is implicitly static (ยง9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies Mike, I only saw your reply once I had submitted mine. I ain't gonna argue with the JLS
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I should have noticed the proximity in timestamps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic