在Java中使用NULL对象访问静态字段 [英] Accessing a static field with a NULL object in Java

查看:156
本文介绍了在Java中使用NULL对象访问静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单的代码片段工作正常并且正在访问带有null对象的静态字段。

  final class TestNull 
{
public static int field = 100;

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

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






在上面的代码中,语句 System.out.println(new TestNull()。temp()。field); 其中静态字段 字段 与NULL对象新的TestNull()。temp()相关联,仍然返回 100 而不是在Java中抛出空指针异常!为什么?

解决方案

与常规成员变量相反,静态变量属于类而不是班级的实例。它的工作原因仅仅是因为你不需要需要一个实例来访问一个静态字段。



其实我是如果访问静态字段可能会抛出 NullPointerException ,那么我会更令人惊讶



<如果你很好奇,这里是字节码查找你的程序:

  //创建TestNull对象
3 :新#3; // class TestNull
6:dup
7:invokespecial#4; //方法TestNull。< init>:()V

//调用temp方法
10:invokevirtual#5; //方法TestNull.temp :()LTestNull;

//丢弃对temp的调用结果。
13:pop

//加载静态字段的内容。
14:getstatic#6; // Field TestNull.field:I






这是在 Java语言规范,第15.11.1节:使用主要。它们甚至提供了一个示例:


以下示例演示一个空引用可用于访问类(静态)变量,不会导致异常:

  class Test {
static String mountain =Chocorua;
static测试收藏夹(){
System.out.print(Mount);
返回null;
}
public static void main(String [] args){
System.out.println(favorite()。mountain);
}
}

它编译,执行和打印:



Mount Chocorua



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); in which the static field field is being associated with a NULL objectnew TestNull().temp(), still it is returning a correct value of it which is 100 instead of throwing a null pointer exception in 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.

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. 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

这篇关于在Java中使用NULL对象访问静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆