通过 Java 中的对象引用访问静态变量

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

Accessing a static variable via an object reference in Java

javascopestatic

提问by TU_HEO DAKAI

Why can we access a static variable via an object reference in Java, like the code below?

为什么我们可以通过 Java 中的对象引用访问静态变量,如下面的代码?

public class Static {
    private static String x = "Static variable";

    public String getX() {
        return this.x;                 // Case #1
    }

    public static void main(String[] args) {
        Static member = new Static();
        System.out.println(member.x);  // Case #2
    }   
}

采纳答案by kba

Generally, public variables can be accessed by everybody, and private variables can only be accessed from within the current instance of the class. In your example you're allowed to access the xvariable from the mainmethod, because that method is within the Static class.

一般来说,公共变量可以被所有人访问,而私有变量只能从类的当前实例内部访问。在您的示例中,您可以x从该main方法访问该变量,因为该方法位于 Static 类中。

If you're wondering why you're allowed to access it from another instance of Static class than the one you're currently in (which generally isn't allowed for private variables), it's simply because static variables don't exist on a per-instance basis, but on a per class basis. This means that the same static variable of Acan be accessed from all instances of A.

如果您想知道为什么允许您从静态类的另一个实例而不是您当前所在的实例访问它(通常不允许私有变量),这仅仅是因为静态变量不存在于每个实例的基础上,但在每个类的基础上。这意味着可以从A 的所有实例访问A的相同静态变量。

If this wasn't the case, nobody would be able to access the private static variable at all, since it doesn't belong to oneinstance, but them all.

如果不是这种情况,则根本没有人能够访问私有静态变量,因为它不属于一个实例,而是属于所有实例。

回答by Chandra Sekhar

static variables are otherwise called as class variables, because they are available to each object of that class.

静态变量也称为类变量,因为它们可用于该类的每个对象。

As member is an object of the class Static, so you can access all static as wll as non static variables of Static class through member object.

由于成员是静态类的对象,因此您可以通过成员对象访问静态类的所有静态变量以及非静态变量。

回答by emory

It is not best practice to reference a static variable in that way.

以这种方式引用静态变量不是最佳做法。

However your question was why is it allowed? I would guess the answer is to that a developer can change an instance member (field or variable) to a static member without having to change all the references to that member.

但是你的问题是为什么允许?我猜答案是开发人员可以将实例成员(字段或变量)更改为静态成员,而无需更改对该成员的所有引用。

This is especially true in multi-developer environments. Otherwise your code may fail to compile just because your partner changed some instance variables to static variables.

在多开发人员环境中尤其如此。否则,您的代码可能无法编译,因为您的合作伙伴将某些实例变量更改为静态变量。

回答by Awan Biru

The non-static member is instance member. The static member(class wide) could not access instance members because, there are no way to determine which instance owns any specific non-static members.

非静态成员是实例成员。静态成员(类宽)无法访问实例成员,因为无法确定哪个实例拥有任何特定的非静态成员。

The instance object could always refers to static members as it belongs to class which global(shared) to its instances.

实例对象总是可以引用静态成员,因为它属于全局(共享)到其实例的类。

回答by Stephen C

The reason that it is allowed is that the JLS says it is. The specific sections that allows this are JLS 6.5.6.2(for the member.xcases) and JLS 15.11.1(in both cases). The latter says:

允许的原因是 JLS 说它是。允许这样做的特定部分是JLS 6.5.6.2(对于这些member.x情况)和JLS 15.11.1(对于这两种情况)。后者说:

If the field is static:

  • If the field is a non-blank final field, then the result is the value of the specified class variable in the class or interface that is the type of the Primary expression.

  • If the field is not final, or is a blank final and the field access occurs in a class variable initializer (§8.3.2) or static initializer (§8.7), then the result is a variable, namely, the specified class variable in the class that is the type of the Primary expression.

如果字段是静态的:

  • 如果该字段是非空的 final 字段,则结果是作为 Primary 表达式类型的类或接口中指定的类变量的值。

  • 如果该字段不是 final,或者是一个空白 final 并且字段访问发生在类变量初始化器(第 8.3.2 节)或静态初始化器(第 8.7 节)中,则结果是一个变量,即指定的类变量在作为 Primary 表达式类型的类。



Why are these allowed by the JLS?

为什么 JLS 允许这些?

Frankly, I don't know. I can't think of any good reasons to allow them.

坦白说,我不知道。我想不出有什么好的理由允许他们。

Either way, using a reference or thisto access a static variable is a bad idea because most programmersare likely to be mislead into thinking that you are using an instance field. That is a strong reason to not use this feature of Java.

无论哪种方式,使用引用或this访问静态变量都是一个坏主意,因为大多数程序员可能会误以为您正在使用实例字段。这是不使用 Java 的这个特性的一个强有力的理由。

In your first and second cases you should reference the variable as xor Static.xrather than member.x. (I prefer Static.x.)

在第一种和第二种情况下,您应该将变量引用为xStatic.x而不是member.x。(我更喜欢Static.x。)

回答by WailenB

This logically makes sense although it is not interesting practice. Static variable is usually for enforcing single declaration of variable during instantiation. Object is a new copy of Class with other name. Even though object is new copy of class it is still with characteristics of the (uninstantiated) Class (first invisible instance). Therefore new object also has that static members pointing to the original copy. Thing to note is: New instance of StackOverflow is also StackOverflow.

这在逻辑上是有道理的,尽管这不是有趣的做法。静态变量通常用于在实例化期间强制执行变量的单一声明。对象是具有其他名称的类的新副本。即使对象是类的新副本,它仍然具有(未实例化的)类(第一个不可见实例)的特征。因此,新对象也具有指向原始副本的静态成员。需要注意的是:StackOverflow 的新实例也是 StackOverflow。