aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A member interface is implicitly static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A member interface is implicitly static" Watch "A member interface is implicitly static" New topic
Author

A member interface is implicitly static

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
"A member interface is implicitly static"

Can anybody give me some logical reasoning for this statement so that I can understand.


SCJP 1.4, SCBCD 1.3
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

"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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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).


Tony Morris
Java Q&A (FAQ, Trivia)
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I misunderstood the question. Yes member variables in interface are static and final implicitly.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Oct 12, 2000
Posts: 5093
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).


42
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I think it would be appropriate to call the variables in a interface as constants rather than member variables.

http://www.coderanch.com/t/245071/java-programmer-SCJP/certification/Non-final-member-variables-interfaces

and

http://www.coderanch.com/t/248449/java-programmer-SCJP/certification/final-static-int-Whizlabs-says
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.)


"I'm not back." - Bill Harding, Twister
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
thanx Jim Yingst for clearifying my question.
thanx Jeroen Wenting for a good example.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Feb 18, 2002
Posts: 64
Tony,

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


Regards,
Sumit


SCJP 1.2, SCJP 1.4, SCJA, SCWCD 1.4<br /> <br />------------------------------<br />
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Nov 15, 2004
Posts: 1026

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

Joined: Sep 24, 2003
Posts: 1608

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

Joined: Nov 15, 2004
Posts: 1026
Jim please reply to this post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A member interface is implicitly static