• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variables in interface

 
Ranch Hand
Posts: 33
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds like setting them to null as if you could use function pointers (as in C).
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone. I understood clearly. Thanks for the links too.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic