Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

Interfaces - Very surprising

 
Stephen King
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Report post to moderator
As per Java Language Specification,

9.3 Field (Constant) Declarations

ConstantDeclaration:

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
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi stephen,

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


thanks & regards,

seetharaman
 
shibaram sahoo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
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 ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Shibaram,
Please don't post the same question in multiple threads. It makes conversation difficult to follow. Please see other thread
 
    Bookmark Topic Watch Topic
  • New Topic