简体   繁体   English

在 Java 中使用 NULL 对象访问静态字段

[英]Accessing a static field with a NULL object in Java

The following simple code snippet is working fine and is accessing a static field with a null object.以下简单的代码片段工作正常,并且正在访问具有空对象的静态字段。

final class TestNull
{
    public static int field=100;

    public TestNull temp()
    {
        return(null);
    }
}

public class Main
{
    public static void main(String[] args)
    {
        System.out.println(new TestNull().temp().field);
    }
}

In the above code, the statement System.out.println(new TestNull().temp().field);在上面的代码中,语句System.out.println(new TestNull().temp().field); in which the static field field is being associated with a NULL object new TestNull().temp() , still it is returning a correct value of it which is 100 instead of throwing a null pointer exception in Java!其中静态字段字段与 NULL 对象new TestNull().temp() ,它仍然返回正确的值,即100,而不是在 Java 中抛出空指针异常! Why?为什么?

As opposed to regular member variables, static variables belong to the class and not to the instances of the class.与常规成员变量相反,静态变量属于类而不属于类的实例。 The reason it works is thus simply because you don't need an instance in order to access a static field.它工作的原因很简单,因为您不需要实例来访问静态字段。

In fact I'd say it would me more surprising if accessing a static field could ever throw a NullPointerException .事实上,如果访问静态字段可能抛出NullPointerException我会说更令人惊讶

If you're curious, here's the bytecode looks for your program:如果你很好奇,这里是寻找你的程序的字节码:

// Create TestNull object
3: new             #3; //class TestNull
6: dup
7: invokespecial   #4; //Method TestNull."<init>":()V

// Invoke the temp method
10: invokevirtual   #5; //Method TestNull.temp:()LTestNull;

// Discard the result of the call to temp.
13: pop

// Load the content of the static field.
14: getstatic       #6; //Field TestNull.field:I

This is described in the Java Language Specification, Section 15.11.1: Field Access Using a Primary .这在Java Language Specification, Section 15.11.1: Field Access Using a Primary 中有描述 They even provide an example:他们甚至提供了一个例子:

The following example demonstrates that a null reference may be used to access a class (static) variable without causing an exception:以下示例演示了可以使用空引用访问类(静态)变量而不会导致异常:

 class Test { static String mountain = "Chocorua"; static Test favorite(){ System.out.print("Mount "); return null; } public static void main(String[] args) { System.out.println(favorite().mountain); } }

It compiles, executes, and prints:它编译、执行和打印:

Mount Chocorua乔科鲁阿山

Static variables are shared among every object of a class.静态变量在类的每个对象之间共享。 So while the actual object reference you are returning is null (In C++: TestNull* temp = null;), you have an object of type TestNull which Java can use to find static values of that class.因此,虽然您返回的实际对象引用为 null(在 C++ 中:TestNull* temp = null;),但您有一个 TestNull 类型的对象,Java 可以使用它来查找该类的静态值。

Remember in Java objects are really pointers.请记住,Java 中的对象实际上是指针。 Pointers have a type.指针有一个类型。 From that type Java can discern certain information, even if its pointing to null.从该类型 Java 可以识别某些信息,即使它指向 null。

Static fields are associated with the class not an instance of that class.静态字段与类相关联,而不是该类的实例。 Therefore you don't need an instance of an object to access the static field.因此,您不需要对象的实例来访问静态字段。

You could also access the field by calling TestNull.field您还可以通过调用 TestNull.field 来访问该字段

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM