This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables in interface

 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know variables declared in interface should be public static and final. But is it implicit? For example



Does the compiler assume that it is public static and final or should it be mentioned explicitly?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the member variables in an interface are by default public static final. That is how interfaces are by design. Also, interfaces are by default abstract. So when you say



the compiler considers it as

 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: . . . . Also, interfaces are by default abstract. . . .
the compiler considers it as

Are you sure about that?The word abstract has vanished from the interface. Did you see that anywhere in the JLS?

It can be dubious design to put constants into interfaces, as you can see here. It is probably better to put those constants into an uninstantiable class.And to answer the original question: you always miss out modifiers in interfaces, so you simply writeIn fact, when you are writing interfaces, the biggest and mostest parts of the interface are the documentation comments
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I read it somewhere. Here you go. abstract keyword as applied to interfaces in Java.

One more interesting thing Ritchie. Earlier(as in way back when Oak was introduced), interface methods were declared as follows:



Later on the "=0" was replaced by ().
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like setting them to null as if you could use function pointers (as in C).
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StackOverflow link is a bit stale; it loads, but several of the pages it references are out of date. Here's a current quote from JLS7 9.1.1.1:
Every interface is implicitly abstract.

This modifier is obsolete and should not be used in new programs.


Additionally, for the original question, from JLS7 9.3:
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. I understood clearly. Thanks for the links too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic