• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Interfaces

 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that the enclosing interface cannot be marked static whereas the enclosed interface can be marked static?

Any comments!!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Enclosing interfaces can be static.
Do you mean a top-level interface? If so, what would a static top-level interface mean to you?
[ July 29, 2005: Message edited by: Barry Gaunt ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprise, interface means something that is abstract and abstract thing can't be static whether it is inner or outer. It was just my thought. But it is not true, am I not thinking in right way??

Thanks.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static interface { //Compiler error - But why??

static interface { //No compiler error
void m1();

}

void m2();

}
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry forgot to add the interface names!!!


static interface outer{ //Compiler error - But why??

static interface inner{ //No compiler error
void m1();

}

void m2();

}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what static *means* for a nested interface?

How would that meaning translate to toplevel interfaces?
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marking a class or an interface static at the top level is only to ensure that it can be accessed without an instance.
So having a interface marked as static at the class level would mean nothing as you can never instantiate your interface.

But..

Marking a interface which is inside a class ensures that the interface can be implemented/extended without having the actual instance of the toplevel enclosing class/interface.

Am I correct barry??
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:
Marking a class or an interface static at the top level is only to ensure that it can be accessed without an instance.
So having a interface marked as static at the class level would mean nothing as you can never instantiate your interface.

But..

Marking a interface which is inside a class ensures that the interface can be implemented/extended without having the actual instance of the toplevel enclosing class/interface.

Am I correct barry??


Neither top-level classes or interfaces can be declared static. What would this mean? Only nested elements can be declared static. Whether its a variable, method, nested class or interface, the meaning is similar: you don't need an instance of the outer class to access the nested element. This means that your later statement is on the mark. A static interface can be implemented or extended without an instance of the outer class or interface.

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic