tags:

views:

113

answers:

4

Why is that a variable used in an Interface is PUBLIC STATIC FINAL? Why "static" in particular?

+1  A: 

Because you can not instantiate an interface. Also there cannot be any method body to use a non-static non-final variable.

fastcodejava
Great answer! Thank you :)
Sandeep
+1  A: 

Why wouldn't it be static?

It's a constant associated with the interface, rather than with any particular instance of it.

Anon.
+6  A: 

A field declared in an interface can only be a constant anyway, so why would it depend on which instance you use to access it?

Putting fields in interfaces is often poor style anyway these days. The interface is meant to reflect the capabilities of classes that implement it - which is completely orthogonal to the idea of a constant. It's certainly a nasty idea to use an interface just to declare a bunch of constants. I do occasionally find it useful to make the interface type expose constants which are simple implementations - so a filtering interface might have "ALLOW_ALL" and "ALLOW_NONE" fields, for example.

I suppose you could conceive of a scenario where implementing an interface did actually add an instance field to your class - but that would break encapsulation not only in terms of it being implicitly public, but also by specifying part of the implementation instead of the API.

Jon Skeet
Static constant parameters are bad as a whole anyway nowadays, replacing bunch of constants with Enums is the proper Java way currently, since it's extremely typesafe, works with switches and can be coded to include multiple (helpful) values to one constant essentially avoiding repetitive magic numbers.
Esko
@Jon Skeet Great.. But I wasn't implementing it practically!
Sandeep
@Esko: Enums are appropriate in *some* cases, but they certainly don't cover *every* case where you'd want constants. In particular, enums are only appropriate where there's a fixed set of values. For example, it would be nice to have Charset "constants" for UTF-8 and the other guaranteed Charsets - but you wouldn't want to make that an enum. Don't try to make everything look like a nail just because you've got the enum hammer :)
Jon Skeet
That's mostly because charsets generally aren't static nor constant if you consider their behaviour :) I'm very willing to bet that if something's truly constant as a concept, it can easily be replaced with an enum.
Esko
A: 

The main reason I guess is implementation detail of the VM/language.

If an interface is not allowed to have non-static variables, there's no need to allocate memory for the interface during the creation of the class. There's also no need for special naming/renaming mechanisms in case you inherit variables with the same name. The only thing you need is some table to call the correct functions when the interface is used.

In short - it makes the live of the language / VM maintainer easier. If you really want to take a look at multiple inheritance and its pitfalls and traps, read Object Oriented Software Construction by Bertrand Meyer (2nd Edition). Then you understand why the interface needs to be so simple (and yet archives most of the things multiple inheritance does).

Tobias Langner
Nice answering! Really helped..
Sandeep