tags:

views:

501

answers:

5

Why are interface variables static and final by default in Java?

A: 

What meaning would an instance of an interface have? By DEFINITION an interface is abstract and cannot be instantiated. Thus no instance variables, static only.

JUST MY correct OPINION
Abstract classes can have instance variables as well as implemented functions. Interfaces can't.
dan04
Isn't it a coincidence, then, that I didn't mention abstract classes?
JUST MY correct OPINION
he's pointing out that abstract classes are by definition abstract and cannot be instantiated. But somehow they *can* have instance variables. Your reasoning is unsound.
Philip Potter
A: 

Because anything else is part of the implementation, and interfaces cannot contain any implementation.

Amir Afghani
then what is the reason for final.
Jothi
To indicate that its a constant. Java doesn't have a const keyword. static final is how you declare constants.
Amir Afghani
+2  A: 

From www.codestyle.org:

Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.

cherouvim
A: 

static - because Interface cannot have any instance. and final - because we do not need to change it.

Webbisshh
+1  A: 

Because Sun incorrectly decided developers didn't need a const keyword.

Spencer Ruport