• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A member interface is implicitly static

 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A member interface is implicitly static"

Can anybody give me some logical reasoning for this statement so that I can understand.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"A member interface is implicitly static"


I don't think this is correct.

You can say "A member interface is implicitly abstract"

This makes some sense. Because method declarations in interfaces are by default Abstract.

Modifier "abstract" can be used for top level interface declaration. But such practices are strongly discouraged.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A member is indeed implicitly static.
This just means that it is static without explicitly specifying the static modifier. Good form also mandates that you should never explicitly specify redundant modifiers (that apply to all type declarations (class, interface, annotation, enum) and their members, not just interfaces).
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I misunderstood the question. Yes member variables in interface are static and final implicitly.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constants in interface can be hidden by subinterfaces.


In the case of multiple inheritance of interface constants, any name conflicts can be resolved using fully qualified names in the constants involved.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too read it as meaning an interface which is a member of a class being implicitly static.

Something like


Would the interface SomeInterface be static here?
If it were the following would work:


And somewhat to my surprise it indeed works, proving that member interfaces are implicitly static (in contrast to inner classes).
 
Vishnu Prakash
Ranch Hand
Posts: 1026
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, to be clear - the original question was not about members inside an interface. It's about the interface itself, assuming it's nested inside another class or interface. A member interface is an interface that is a member of some other class or interface - and yes, member interfaces are implicitly static. (Mostly because there's no real use for a "this" reference inside an interface, so there's be no benefit to having a non-static interface.)
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Jim Yingst for clearifying my question.
thanx Jeroen Wenting for a good example.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A member interface is implicitly static.


An interface member is not a constant - it is public static and final. This is not the same as a constant. A final is not synonymous with a constant.
 
John Wolf
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,

Could you expand on why
A final is not synonymous with a constant


Regards,
Sumit
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, something like

is clearly final, but it's not constant since anyone can change the contents of the StringBuffer at any time. The precise rules for what constitues a compile-time constant in Java are given in JLS 15.28.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll do more than that, I'll give you an example:

When a final is not a constant
http://qa.jtiger.org/GetQAndA.action?qids=13

You should note that this kind of question will not be on your exam.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

An interface member is not a constant - it is public static and final. This is not the same as a constant.


In Kathy Sierra & Bert Bates book it is given as(Page 115)

All variables defined in an interface must be public, static and final - in other words interfaces can declare only constants and not instance variables.

Can u both confirm this.

Tony your example is phenomenal.

Jim the example which u had given helps to understand it is not necessary for a final variable to be constant all the time. But I want to know whether a interface variable which is implicitly final is a constant or not.

Jim if you can give simple examples for these statements it will of great help.

# Simple names that refer to final variables whose initializers are constant expressions
# Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All variables defined in an interface must be public, static and final - in other words interfaces can declare only constants and not instance variables.

Can u both confirm this.

I can confirm that this is incorrect.
Books are wrong all the time - they are not an eternal truth.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim please reply to this post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic