Organizational Research By

Surprising Reserch Topic

why are interface variables static and final by default

why are interface variables static and final by default  using -'java,interface'

asked Sep 10, 2015 by Charley2110
0 votes

Related Hot Questions

9 Answers

0 votes

From the Java interface design FAQ by Philip Shaw:

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.


answered Sep 10, 2015 by EssieColbert
0 votes

Everybody is saying why they are static, that is ok, but why final...all are saying to make a true constant. But why??? I think in the case if our interface is extended by two new interfaces and these two interfaces are implemented by some class and if our variable is not a true constant then we will have old typical problem of diamond.

answered Sep 10, 2015 by VerUDY
0 votes

Since interface doesn't have a direct object, the only way to access them is by using a class/interface and hence that is why if interface variable exists, it should be static otherwise it wont be accessible at all to outside world. Now since it is static, it can hold only one value and any classes that extends it can change it and hence it will be all mess.

Hence if at all there is an interface variable, it will be implicitly static and final and obviously public!!!

answered Sep 10, 2015 by Cathryn8433e
0 votes

Java does not allow abstract variables and/or constructor definitions in interfaces. Solution: Simply hang an abstract class between your interface and your implementation which only extends the abstract class like so:

 public interface IMyClass {

     void methodA();
     String methodB();
     Integer methodC();


 public abstract class myAbstractClass implements IMyClass {
     protected String varA, varB;

     myAbstractClass(String varA, String varB) {
         this.varA = varA;
         this.varB = VarB;

     //Implement (some) interface methods here or leave them for the concrete class
     protected void methodA() {
         //Do something

     //Add additional methods here which must be implemented in the concrete class
     protected abstract Long methodD();

     //Write some completely new methods which can be used by all subclasses
     protected Float methodE() {
         return 42.0;


 public class myConcreteClass extends myAbstractClass {

     //Constructor must now be implemented!
     myClass(String varA, String varB) {
         super(varA, varB);

     //All non-private variables from the abstract class are available here
     //All methods not implemented in the abstract class must be implemented here


You can also use an abstract class without any interface if you are SURE that you don't want to implement it along with other interfaces later. Please note that you can't create an instance of an abstract class you MUST extend it first.

(The "protected" keyword means that only extended classes can access these methods and variables.)


answered Sep 10, 2015 by LonnaWarfiel
0 votes

So far, I hadn´t understood that interfaces can be used as data types in java. I discovered it while learning depency injection. An interface in java is a valid referenca data type. If you want to use an interface as reference variabile, make sure your class implements it! See: Oracle java tutorial

answered Sep 10, 2015 by MarCqk
0 votes

In Java, interface doesn't allow you to declare any instance variables. Using a variable declared in an interface as an instance variable will return a compile time error.

You can declare a constant variable, using static final which is different from an instance variable.

answered Sep 10, 2015 by Kur29K
0 votes

Interface can be implemented by any classes and what if that value got changed by one of there implementing class then there will be mislead for other implementing classes. Interface is basically a reference to combine two corelated but different for that reason the declaring variable inside the interface will implicitly be final and also static because interface can not be instantiate.

answered Sep 10, 2015 by YvetteToutgy
0 votes

Think of a web application where you have interface defined and other classes implement it. As you cannot create an instance of interface to access the variables you need to have a static keyword. Since its static any change in the value will reflect to other instances which has implemented it. So in order to prevent it we define them as final.

answered Sep 10, 2015 by LilyXMEZstji
0 votes

I think it's because interfaces can't be instantiated, so all variables are declared as static. Use of the final keyword means it doesn't have a body.

answered Sep 10, 2015 by Alp7002