简体   繁体   English

具有空引用的静态变量

[英]Static Variable with null reference

When I run this code it print twice 0 but i don't undrestand why after affect the object k to null the value of count still printed 0 but when i delete static before count and execute this program it print first 0 and then i print an exception Exception in thread "main" java.lang.NullPointerException. 当我运行这段代码时,它打印两次0但是我没有动摇,为什么在影响对象k后将计数值仍然打印为0但是当我在计数之前删除静态并执行此程序时它首先打印0然后我打印一个异常线程“main”中的异常java.lang.NullPointerException。 Please Can you resolve this problem. 请问你能解决这个问题。

public class Test{
    public static int count=0;
    public static void main(String[] args){
        Test t = new Test();
        System.out.println(t.count); // 0
        t=null;
        System.out.println(t.count); // 0
    }
}

static variables in Java are defined at class level and you do not need an object of that class to reference static variable. Java中的static变量是在类级别定义的,您不需要该类的对象来引用静态变量。 And even if you write t.count JVM will instead do conventional Test.count instead (it will replace name of variable with name of its class). 即使你写了t.count JVM也会替代传统的Test.count (它将用其类的名称替换变量的名称)。

And below extract from JLS: https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.11.1 以下摘自JLS: https//docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.11.1

Example 15.11.1-2. 例15.11.1-2。 Receiver Variable Is Irrelevant For static Field Access 接收器变量与静态现场访问无关

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

class Test3 { static String mountain = "Chocorua"; class Test3 {static String mountain =“Chocorua”; static Test3 favorite(){ System.out.print("Mount "); static Test3 favorite(){System.out.print(“Mount”); return null; return null; } public static void main(String[] args) { System.out.println(favorite().mountain); public static void main(String [] args){System.out.println(favorite()。mountain); } } It compiles, executes, and prints: 它编译,执行和打印:

Mount Chocorua Even though the result of favorite() is null, a NullPointerException is not thrown. Mount Chocorua尽管favorite()的结果为null,但不会抛出NullPointerException。 That "Mount " is printed demonstrates that the Primary expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field mountain is static). 打印“Mount”表明主表达式确实在运行时完全评估,尽管事实上只使用其类型而不是其值来确定要访问的字段(因为字段山是静态的)。

I assume, you meant t=null; 我假设,你的意思是t=null; instead of k=null; 而不是k=null; .

The static variables are there always only once in whole program, they aren't bent to the objects. 静态变量在整个程序中始终只有一次,它们不会弯曲到对象。 All instances of Test have the same count, always. 所有Test实例始终具有相同的计数。 If you set it to anything else, it will change for all instances. 如果将其设置为其他任何内容,它将针对所有实例进行更改。 Therefore, you can read the value from null as well. 因此,您也可以从null读取值。 Alternativelly, you can use System.out.println(Test::count) , which will also print 0, without the need of any object of class Test. 或者,您可以使用System.out.println(Test::count) ,它也将打印0,而不需要任何类Test对象。

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

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