java 接口的成员变量必须是最终的......为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11981145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:09:18  来源:igfitidea点击:

Member Variables of an interface must be final... Why?

javainterfacestaticfinal

提问by AnkitChhajed

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中的成员变量不能是非常量..静态的逻辑在我的脑海中是正确的,如果需要访问Interface的变量,那么它是必须的是静态的,因为我们不能创建接口的实例,但是为什么需要 final 呢??下面的代码显示了接口成员变量是如何被设为静态最终的,即使我们默认没有提到它......

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

提前致谢 !!!

回答by

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.

接口不是一个类,它是一套规则,不能被实例化,那么它里面就不能包含任何易变的数据容器。尽管不鼓励在接口中设置常量,但只能在接口内设置常量,因为在接口中声明常量违反了封装方法

回答by AnkitChhajed

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.

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

回答by SpiXel

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 接口本身无法实例化;变量的值必须在不存在实例的静态上下文中分配。final 修饰符确保分配给接口变量的值是一个真正的常量,不能由程序代码重新分配。并记住,接口用于显示您必须实现的“什么”而不是如何实现。所以变量应该是最终的(因为非静态变量与类的整个规范无关)。

回答by Prakhar Mohan Srivastava

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'ssomething on interfaces. Hope it helps.

Java 成员变量默认必须是 final 的,因为接口不应该被实例化。默认情况下,它们也是静态的。因此,您不能更改它们的值,也不能在分配后重新分配它们。这里有一些关于接口东西。希望能帮助到你。

回答by user3678240

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

Java- 没有实现多重继承但是通过接口我们可以实现。

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.

现在因为它是静态的,它应该是最终的,因为如果它不是最终的,那么任何实现它的类都会改变这个值,而其他实现接口的类将收到改变的值。例如,如果类 x 将 r 设为静态而不是最终的。

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