简体   繁体   中英

Member Variables of an interface must be final… Why?

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 !!!

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 .

对于成员变量,我认为必须是静态的,因为无法为接口创建对象,因此要访问成员变量,需要将其静态化并通过类访问它。

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 ).

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.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM