aspose file tools
The moose likes Beginning Java and the fly likes Interfaces - Very surprising Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Interfaces - Very surprising

Stephen King

Joined: Jul 08, 2007
Posts: 23
Dear ranchers,

Just came across the below statement/rule in the java documentation -
Every field declaration in the body of an interface is implicitly public, static, and final.

Can somebody on this forum please share what is the rational behind allowing only the modifiers public, static and final???

Why I can not have a private/protected field in an interface?? I have many requirements which perfectly call for such a field!

thanks in advance for your views.

~ Steve

Believe in yourself!
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
As per Java Language Specification,

9.3 Field (Constant) Declarations


ConstantModifiers Type VariableDeclarator

ConstantModifiers: one of

public static final

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted, but strongly discouraged as a matter of style, to redundantly specify any or all of these modifiers for such fields.

A constant declaration in an interface must not include any of the modifiers synchronized, transient, or volatile, or a compile-time error occurs.

The reason for this is Interface is a contract which can be used by different classes. They are not a means of storage. Hence they cannot have fields of their own. But they can have constants or standard values where the values never change. If a field value is required, use abstract class.
[ March 10, 2008: Message edited by: Sripathi Krishnamurthy ]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19971

Variables are part of the implementation, not the interface. As interfaces can have no implementation variables are not allowed. As Sripathi suggested, use abstract classes if you want a base set of shared variables.

How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hi stephen,

interface is a contract,..mainly for public,methods are public variables are public+final....

thanks & regards,

shibaram sahoo

Joined: Sep 21, 2005
Posts: 17
I have also a similar kind of confusion in my mind..
Please help me out..

Interface is a contract. (I agree)
Interface are means to be used to create generic behaviours (methods) and attributes (variables), so that implementor classes will get through inheritance. (I agree and believe on this)

But my confusion is that, If I my classes gonna get an attribute from an interface, then I can not modify it as per my class requirement.

So why do have all the variables are by default public static final ?
Isn't it would have better like if just public / protected variables (no static)

Then I could have easily get the color through inheritance and whenever I want to change color of my car I could have done that easily..

what do you say..

why do have all the variables are by default public static final, then ?

[ August 29, 2008: Message edited by: shibaram sahoo ]
[ August 29, 2008: Message edited by: shibaram sahoo ]

We can do it !!
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31808

Please don't post the same question in multiple threads. It makes conversation difficult to follow. Please see other thread

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Consider Paul's rocket mass heater.
subject: Interfaces - Very surprising