This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Variables in interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variables in interface" Watch "Variables in interface" New topic
Author

Variables in interface

Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

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

Joined: Jul 27, 2010
Posts: 1157

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



~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37865
    
  22
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 37865
    
  22
That sounds like setting them to null as if you could use function pointers (as in C).
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
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

Joined: Feb 12, 2013
Posts: 33

Thanks everyone. I understood clearly. Thanks for the links too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variables in interface
 
Similar Threads
interfaces
Strange question
Inheritance
same var in parent and child interface
Interface