java 接口可以保存任何实例变量吗?

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

Can an interface hold any instance variables?

java

提问by Nikolay

Can an interface hold any instance variables? In my instructor's specs, it says no instance variables are allowed in interface. But I've researched and some say that it can only contain finalinstance variable. If it can hold a finalinstance variable, so what role can that variable play at all in an interface? Thanks in advance!

接口可以保存任何实例变量吗?在我的讲师规范中,它说接口中不允许使用实例变量。但我研究过,有人说它只能包含final实例变量。如果它可以保存一个final实例变量,那么该变量在接口中到底能起到什么作用呢?提前致谢!

回答by Aniket Thakur

variables declared in interface are by default public, staticand final. Since it is static you cannot call it instance variable.

在 interface 中声明的变量默认为public,staticfinal. 由于它是静态的,您不能将其称为实例变量。

回答by Ranjeet

  • Variables which declared in interface are by default public, static and final.

  • These are static so you cannot call it as instance variable.

  • 在 interface 中声明的变量默认是 public、static 和 final。

  • 这些是静态的,因此您不能将其称为实例变量。

回答by user85

By default interface variables will always be public static finalwhether you mention these modifiers or not while defining variables. So, you can never have an instance variable in an interface.

默认情况下,无论您在定义变量时是否提及这些修饰符,接口变量都将始终为public static final。因此,您永远不能在接口中拥有实例变量。

回答by Gihan Saranga Siriwardhana

Variables declared in an interface are by default public, staticand finalby default. So you can use interfaces to define constants.

在接口中声明的变量默认情况下publicstatic并且final在默认情况下。所以你可以使用接口来定义常量。

You can read more about this Here

您可以在此处阅读有关此内容的更多信息

enter image description here

在此处输入图片说明

回答by Vishal Sheth

At first we need to understand what is the role of interface. As such interface has by default abstract modifier.

首先我们需要了解接口的作用是什么。因此,接口默认具有抽象修饰符。

1) So no object can be created for interface. As such object can not be created no role of instance variable in interface.

1) 所以不能为接口创建对象。因此无法创建对象,在接口中没有实例变量的作用。

No object can be created for interface so no constructor supported for same.

不能为接口创建对象,因此不支持相同的构造函数。

2) Interface variables which declared are by default public, static and final and must need initialize at the time of declaration.

2) 声明的接口变量默认是public、static和final,必须在声明时初始化。

3) With Static you cannot call it as instance variable you can call it by.

3)使用静态,您不能将其称为实例变量,您可以通过它来调用它。

interface_filename.interface_variablename

No interface cannot have instance variable.

没有接口不能有实例变量。

回答by Himanshu Ahuja

Interface doesnt hold by itself instance variables of its own as by default inside interface variables are static and final. But can show the same purpose when implementing an interface as the interface object/instance is made is of class type.

接口本身并不包含自己的实例变量,因为默认情况下接口内部的变量是静态的和最终的。但是在实现接口时可以显示相同的目的,因为接口对象/实例是类类型的。

It can be said as object referenced variables via interface.

可以说是通过接口引用对象的变量。

    class sample implements interfacee {
String xyz = "hello";
      }

回答by Balamurali Arumugam

Variable declared inside Interface are public, static, final(by default) making it not an instance variable for Interface.

在接口中声明的变量是公共的、静态的、最终的(默认情况下)使其不是接口的实例变量。

Eg:

例如:

Interface abc{int a; // compiler will ask to assign value, since it is final}