views:

440

answers:

4

I am just trying to understand why all fields defined in an Interface are implicitly static and final. The idea of keeping fields static makes sense to me as you can't have objects of an interface but why they are final (implicitly)?

Any one knows why Java designers went with making the fields in an interface static and final?

+3  A: 

The fields must be static because they can't be abstract (like methods can). Because they can't be abstract, the implementers will not be able to logically provide the different implementation of the fields.

The fields must be final, I think, because the fields may be accessed by many different implementers allows they to be changeable might be problematic (as synchronization). Also to avoid it to be re-implemented (hidden).

Just my thought.

NawaMan
+1 for the nice explanation
peakit
NawMan, your explanation about "The felds must be static..." does not make much sense. But you were very right abt the "The fields must be final..."
peakit
I don't think he is right about the reason why the fields must be final. Allowing different implementers to change a field is not problematic, since otherwise inheritance would be problematic. Fields must be final, as Adriaan said, because an interface is, and should, be stateless. An interface with a state basically should be an abstract class.
Axelle Ziegler
If you have a `public static` field that is not `final`, findbugs will complain (rightly!).
Tom Hawtin - tackline
+7  A: 

An interface can't have behavior or state because it is intended to specify only an interaction contract, no implementation details. No behavior is enforced by not allowing method/constructor bodies or static/instance initializing blocks. No state is enforced by only allowing constants. A constant in Java is defined by a static final field (and by convention the name uses UPPER_CASE_AND_UNDERSCORES).

Adriaan Koster
+1: I think your reason sounds very logical (better than mine).
NawaMan
It's not necessarily true that final fields are constants; that's only guaranteed for primitive types. In general, the final keyword merely means that the memory location will not change.
Lord Torgamus
A: 

There are a couple of points glossed over here:

Just because fields in an interface are implicitly static final does not mean they must be compile-time constants, or even immutable. You can define e.g.

interface I {
  String TOKEN = SomeOtherClass.heavyComputation();
  JButton BAD_IDEA = new JButton("hello");
}

(Beware that doing this inside an annotation definition can confuse javac, relating to the fact that the above actually compiles to a static initializer.)

Also, the reason for this restriction is more stylistic than technical, and a lot of people would like to see it be relaxed.

Jesse Glick
A: 

prove that the feilds in an interface are implicitly state and final?


 then  i had answered that to question as that by keeping all methods  in interface and implementing the methods in another class whether my answer is corrcet or wrong please tell me its very urgent
svs.raghavendra