Java 为什么默认情况下接口变量是 static 和 final 的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2430756/
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-08-13 07:31:57  来源:igfitidea点击:

Why are interface variables static and final by default?

javainterface

提问by Jothi

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

为什么 Java 中的接口变量默认是 static 和 final 的?

采纳答案by cherouvim

From the Java interface design FAQ by Philip Shaw:

来自 Philip Shaw 的 Java 界面设计常见问题解答:

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.

接口变量是静态的,因为 Java 接口本身无法实例化;变量的值必须在不存在实例的静态上下文中分配。final 修饰符确保分配给接口变量的值是一个真正的常量,不能由程序代码重新分配。

source

来源

回答by Amir Afghani

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

因为其他任何东西都是实现的一部分,接口不能包含任何实现。

回答by RubyDubee

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

静态 - 因为接口不能有任何实例。最后——因为我们不需要改变它。

回答by dganesh2002

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 implements 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, final and obviously public!!!

因此,如果有一个接口变量,它将是隐式静态的,最终的,显然是公共的!!!

回答by spyro

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:

Java 不允许在接口中使用抽象变量和/或构造函数定义。解决方案:只需在您的接口和您的实现之间挂一个抽象类,它只扩展抽象类,如下所示:

 public interface IMyClass {

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

 }

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

     //Constructor
     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 withoutany 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.)

(“protected”关键字意味着只有扩展类才能访问这些方法和变量。)

spyro

斯派罗

回答by Giri

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.

Java, interface 中不允许您声明任何实例变量。使用在接口中声明的变量作为实例变量将返回编译时错误。

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

您可以声明一个常量变量,使用static final它不同于实例变量。

回答by user3889476

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 entity.so for that reason the declaring variable inside the interface will implicitly be final and also static because interface can not be instantiate.

接口可以由任何类实现,如果该值被其中一个实现类改变了,那么会对其他实现类产生误导。接口基本上是结合两个相关但不同的实体的引用。因此,接口内的声明变量将隐式为最终变量,也是静态的,因为接口无法实例化。

回答by srikant

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.

想象一个 Web 应用程序,其中定义了接口并且其他类实现了它。由于您无法创建接口实例来访问变量,因此您需要使用 static 关键字。由于它是静态的,任何值的变化都会反映到其他实现它的实例上。所以为了防止它,我们将它们定义为 final。

回答by Anssi

An Interface is contract between two parties that is invariant, carved in the stone, hence final. See Design by Contract.

接口是两方之间的契约,它是不变的,刻在石头上,因此是最终的。请参阅合同设计

回答by Asif Mushtaq

public interface A{
    int x=65;
}
public interface B{
    int x=66;
}
public class D implements A,B {
    public static void main(String[] a){
        System.out.println(x); // which x?
    }
}

Here is the solution.

这是解决方案。

System.out.println(A.x); // done

I think it is the one reason why interface variable are static.

我认为这是接口变量是静态的原因之一。

Don't declare variables inside Interface.

不要在接口内声明变量。