8

I have a question in my mind, Why can't be a member variable in Interface be a non constant.. The logic of being static stood right in my mind that if one needs to access the variable of Interface then it is must for it to be static as we cannot create the instance of the Interface but Why the need of final arises?? The below code shows how the interface member variables are made static final even though we dont mention it by default....

interface inter{

       int a=10; // It becomes final static by default

       public void interFunc();
} 

class cls implements inter{

       public void interFunc(){

           System.out.println("In Class Method WITH a's Value as --> "+a);
       }
}

class Test{

       public static void main(String[] args){

           inter in= new cls();

           in.interFunc();      
           }
}

Thanks in advance !!!

AnkitChhajed
  • 415
  • 2
  • 6
  • 9

5 Answers5

13

Interface is not a class, it is a set of rules, and cannot be instantiated, then it cannot contain any volatile data container inside. Only constants can be set inside of interface, although it is discouraged, because declaring constants in interfaces violates encapsulation approach.

  • 1
    Unrelated to the question, but I don't see how constant declared in an interface (shared knowledge) violates encapsulation. Unless providing interfaces is also a violation of encapsulation. However, it is all explained in the link: declare as many variables in the interface as required - just use `static import` instead of implementing/extending it. – uvsmtid Jun 19 '16 at 16:30
4

Well for a member variable, i think it is must to be static as the object cannot be created for the interface so to access the member variable one needs to have it static and access it through class.

AnkitChhajed
  • 415
  • 2
  • 6
  • 9
2

Java- Doesnt implement multiple inheritance But by interface we can achieve.

interface i1{  
    int a=1;  
}  

interface i2{  
    int a=2;  
}  

class exampleInterface implements i1,i2{  
    public static void main(String...a){  

        //As interface members are static we can write like this
        //If its not static then we'll write sysout(a) ... which gives ambiguity error.
        //That's why it is static.

        System.out.println(i2.a); 
    }  
}  

Now as it is static it should be final because if its not final then any class implementing it will change the value and other class which is implementing interface will receive changed value. as for example below if class x have r as static not final.

class x{
    static int r=10;
}

class y extends x{
    static void fun(){
        x.r=20;
        System.out.println(x.r);
    }
}

class m extends x{
    void fun(){
        System.out.println(x.r);
    }
}

public class Test{
    public static void main(String[] args) {

        y.fun();
        m obj=new m();
        obj.fun();
    }
}
0

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. and have in mind that an interface is used to show 'what' your going to have to implement not how to. so the variables should be final ( cause the non-static variables are not related to the whole specification of a class ).

SpiXel
  • 4,338
  • 1
  • 29
  • 45
  • 1
    Please include a link to the source for the quote. Also, the markup `> stuff` can be used to quote "stuff". –  Aug 16 '12 at 05:35
  • ok.. Well thanks friend.. This is something good explaination clears some webs out here... :) – AnkitChhajed Aug 16 '12 at 05:54
0

Java member variables have to be final by default as interfaces are not supposed to be instantiated. They are also static by default. So you cannot change their value and also cannot reassign them once they have been assigned. Here's something on interfaces. Hope it helps.

praxmon
  • 5,009
  • 22
  • 74
  • 121