简体   繁体   English

“实例变量”和“接口中声明的变量”之间的区别

[英]Difference between the terms “Instance variable” and “variables declared in Interfaces”

I was reading about Interfaces in a Book and I came upon this Line that confused me. 我正在阅读一本书中的界面,我遇到了困扰我的这条线。

Interfaces are syntactically similar to classes, but they lack instance variables. 接口在语法上与类相似,但它们缺少实例变量。

As far as I know about Interfaces , we can define variables inside an Interface which are by default final . 据我所知, Interfaces ,我们可以在接口内定义默认为final变量。

My question is, What does that Line mean? 我的问题是,那条Line是什么意思? and What is the Difference between an Instance Variable and the Variable defined in the Interface ?? Instance VariableVariable defined in the InterfaceVariable defined in the Interface什么区别?

My question is, What does that Line mean? 我的问题是,那条线是什么意思?

Amongst other things, it means the book's terminology is off. 除此之外,这意味着该书的术语已经关闭。 By "instance variable," they mean "instance field." “实例变量”表示“实例字段”。

An instance field is a field that is specific to an individual instance of a class. 实例字段是特定于类的单个实例的字段。 For example: 例如:

class Foo {
    // Instance field:
    private int bar;

    // Static field:
    public static final int staticBar;
}

The field bar is per-instance, not class-wide. 字段bar是每个实例,而不是类范围。 The field staticBar is class-wide (a static field, sometimes called a "class field"). 字段staticBar是类范围的( 静态字段,有时称为“类字段”)。

Interfaces don't have instance fields. 接口没有实例字段。 They do have static fields. 他们确实有静态字段。 When you do this: 当你这样做:

interface FooInterface {
    int staticBar;
}

staticBar is automatically declared public , static , and final (per JLS §9.3 ). staticBar自动声明为publicstaticfinal (根据JLS§9.3 )。 So that staticBar is roughly equivalent to the one on our Foo class before. 因此staticBar大致相当于我们之前的Foo类。

This means you cant have instance variable but a constant static final variable within an interface as per JLS . 这意味着您不能拥有实例变量,而是根据JLS在接口中使用常量静态最终变量。 For eg 例如

interface MyIface {
    public static final int MY_CONSTANT = 1;
}

And access it using interface name like: 并使用接口名称访问它,如:

int variable = MyIface.MY_CONSTANT;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构造函数和实例变量之间的区别 - Difference between constructor and instance variables 在方法中声明的变量和声明为类变量的字段有什么区别? - What is the difference between a variable declared in a method and a field declared as a class variable? 静态变量与实例变量之间的性能差异 - Performance difference between static and instance variables Java中的局部变量和实例变量有什么区别? - What is the difference between local and instance variables in Java? 在操作和实例变量中强制转换术语 - Casting terms in operations & instance variables 在安全性方面,非最终公共实例和非最终公共实例字段之间的区别? - Difference between non- final public static and non- final public instance fields in terms of security? 使用关键字this和Class name访问静态实例变量之间的区别 - Difference between accessing static instance variables using the keyword this and Class name getter方法和返回实例变量状态的方法之间的区别? - difference between a getter method and a method which returns the state of an instance variable? 创建实例变量和在Java中创建新对象之间的区别? - Difference between creating an instance variable and creating a new object in Java? 在getInstance()方法或实例变量定义中初始化单例是否存在功能差异 - Is there a functional difference between initializing singleton in a getInstance() method, or in the instance variable definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM