This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ]
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.
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 !!