[英]What is the difference between referencing a field by class and calling a field by object?
I have noticed that there are times when coding in Java that I have seen fields called by method: 我注意到在Java中进行编码时,有时会看到方法调用的字段:
System.out.println(object.field);
and by class: 并按类别:
System.out.println(Class.field);
I have not seen any clear distinguishing in my textbooks about what the semantics are for these two cases, and I fear that it is going to be, at least for a noob, a subtle point. 我在教科书中没有看到关于这两种情况的语义的明显区别,而且我担心,至少对于新手来说,这将是一个微妙的问题。 My intuition is that the class calling will be used for static fields? 我的直觉是类调用将用于静态字段? Thanks guys. 多谢你们。 So much con'foo'sion. 这么多的欺骗。
object.field
should be (see note below) an instance member while Class.field
would be a static member. object.field
应该是实例成员(请参阅下面的注释),而Class.field
应该是静态成员。
Note: Like stated by @radai and I think it's worth mentionning, you can also access a static member through an object instance, however that's a very bad practice which is quite misleading. 注意:就像@radai所说的那样,我认为值得一提,您也可以通过对象实例访问静态成员,但是这是一个非常糟糕的做法,具有很大的误导性。
Instance scope versus class scope. 实例范围与类范围。
Check this out: 看一下这个:
class Foobar {
public final int x = 5;
public static final int y = 6;
}
y
is a variable that is only created once, at compile time. y
是在编译时仅创建一次的变量。 It is bound to the class, and therefore shared by all of its instances. 它绑定到该类,因此由其所有实例共享。 You reference this with Foobar.y
. 您可以通过Foobar.y
引用它。
System.err.println(Foobar.y);
x
on the other hand, is an instance variable, and every Foobar
that you create with new
will have a copy of it. 另一方面, x
是一个实例变量,并且您用new
创建的每个Foobar
都会有一个副本。 You would reference it like this: 您可以这样引用它:
Foobar foobar = new Foobar();
System.err.println(foobar.x);
But this will not work: 但这不起作用:
System.err.println(Foobar.x);
My intuition is that the class calling will be used for static fields 我的直觉是,类调用将用于静态字段
Yes SomeClass.field
can be used only if field
is static
. 是的,仅当field
为static
时才可以使用SomeClass.field
。 In this case you can also access it via reference like someClassRef.field
but this code will be changed by compiler to ReferenceType.field
anyway. 在这种情况下,您也可以通过诸如someClassRef.field
引用来访问它,但是无论如何,编译器都会将此代码更改为ReferenceType.field
。 Also it can cause some misunderstandings (it may seem that you are trying to use non-static field) so it is better to use static fields by its class. 它还可能引起一些误解(似乎您正在尝试使用非静态字段),因此最好按类使用静态字段。
If field
is not static then it must belong to some instance so you will have to call it via reference someClassRef.field
如果field
不是静态的,则它必须属于某个实例,因此您必须通过引用someClassRef.field
进行调用
The field Class.field
can be accessed without creating an instance of the class. 可以在不创建类实例的情况下访问Class.field
字段。 These are static
fields which are initialized when the classes are loaded by classloaders. 这些是static
字段,当类加载器加载类时会初始化这些static
字段。
The other field ie object.field
can be accessed only when an instance of the class is created. 仅当创建类的实例时,才能访问另一个字段,即object.field
。 These are instance
field initialized when object of the class is created by calling its constructor. 这些是在通过调用类的构造函数创建类的对象时初始化的instance
字段。
Refrencing a field by class requires the field to be static. 按类刷新字段要求该字段是静态的。
Refrencing a field by object requires the field can be either static or non-static field . 按对象刷新字段要求该字段可以是静态字段,也可以是非静态字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.