简体   繁体   English

按类引用字段和按对象调用字段之间有什么区别?

[英]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 . 是的,仅当fieldstatic时才可以使用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.

相关问题 在引用Java对象时,两者之间有什么区别? - In referencing a java object, what is the difference between the two? 调用与对象与类之间有什么区别 - What's the difference between calling through and object vs class 在方法中声明的变量和声明为类变量的字段有什么区别? - What is the difference between a variable declared in a method and a field declared as a class variable? 局部变量、实例字段、输入参数和类字段有什么区别? - What is the difference between a local variable, an instance field, an input parameter, and a class field? Lucene中Field和StringField有什么区别? - What is the difference between Field and StringField in Lucene? 在 Java 中,通过 getter 和通过变量引用字段之间是否存在性能差异? - In Java is there a performance difference between referencing a field through getter versus through a variable? Java中普通类对象和包装器类对象有什么区别 - What is the difference between a normal class object and a wrapper class object in java 从对象和类本身调用静态函数之间的区别 - Difference between Calling Static function from Object and Class itself 通过Fragment超类或扩展的Fragment类引用片段之间有什么区别? - What the difference between referencing a fragment by the Fragment super class or your extended Fragment class? java.lang.reflect.Field中的toGenericString和toString有什么区别 - What is the difference between toGenericString and toString in java.lang.reflect.Field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM