Why does Java compiler allow static variable acces

2019-01-14 20:47发布

I was pointing some tricks and came across this. In following code:

public class TestClass1 {

    static int a = 10;

    public static void main(String ar[]){
        TestClass1 t1 = null ;
        System.out.println(t1.a); // At this line
    }
}

t1 object is null. Why this code is not throwing NullPointerException?

I know this is not proper way to access static variables but question is about NullPointerException.

7条回答
戒情不戒烟
2楼-- · 2019-01-14 20:54

To add some additional info to the current answers, if you disassemble your class file using:

javap -c TestClass1

You'll get:

Compiled from "TestClass1.java"
public class TestClass1 extends java.lang.Object{
static int a;

public TestClass1();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   aconst_null
   1:   astore_1
   2:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   5:   aload_1
   6:   pop
   7:   getstatic   #3; //Field a:I
   10:  invokevirtual   #4; //Method java/io/PrintStream.println:(I)V
   13:  return

static {};
  Code:
   0:   bipush  10
   2:   putstatic   #3; //Field a:I
   5:   return
}

Here you can see that the access to the static field is done in line 7 by the getstatc instruction. Whenever a static field is accessed through code, a corresponding getstatic instruction will be generated in the .class program file.

*static instructions have the particularity that they don't requiere a reference to the object instance to be in the stack prior to calling them (like, for example invokevirtual which does require an object ref in the stack), they resolve the field/method using just an index to the run time constant pool that will be later used to solve the field reference location.

That's a technical reason for the warning "The static field should be accessed in a static way" that some IDEs will throw at you when you write t1.a, because the object instance is unnecessary to resolve the static field.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-14 20:54

What is the reason of NullPointerException in a code like that:

  TestClass t = null;
  t.SomeMethod();

If SomeMethod is an instance method it'll do something with standard this reference:

  public void SomeMethod() {
    // Here we'll have a NullPointerException (since "this" is null)
    this.SomeField = ... // <- We usually omit "this" in such a code
  }

Since this is null we'll have NullPointerException. If method, field etc. is static it's guaranteed the absense of this reference, so there'll be no NullPointerException

  public static void SomeStaticMethod() {
    // You can't put "this.SomeField = ..." here, because the method is static
    // Ans since you can't address "this", there's no reason for NullPointerException
    ...
  }

  ...

  TestClass t = null; 
  // Equal to "TestClass.SomeStaticMethod();"
  t.SomeStaticMethod(); // <- "this" is null, but it's not addressed
查看更多
ら.Afraid
4楼-- · 2019-01-14 20:57

There is no need for an instance while invoking static member or method.

Since static members belongs to class rather than instance.

A null reference may be used to access a class (static) variable without causing an exception.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#d5e19846

If you see the example (see full example in spec)

 public static void main(String[] args) {
        System.out.println(favorite().mountain); //favorite() returns null
    }

Even though the result of favorite() is null, a NullPointerException is not thrown. 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).

查看更多
不美不萌又怎样
5楼-- · 2019-01-14 20:59

Since a is static compiler converts it to TestClass1.a. For non-static variables it would throw NullPointerException.

查看更多
做自己的国王
6楼-- · 2019-01-14 21:05

t1.a is equivalent to TestClass1.a in this case, since a is a static member (not an instance member). The compiler looks at the declaration of t1 to see what type it is, and then just treats it as if you had used the type name. The value of t1 is never checked. (But if it were a method call, like method(args).a, I think the method would be called. But the return value would be thrown away, and never checked.) (Edit: I've verified that method(args) is called, but no exception is thrown if the function result is null.)

查看更多
放荡不羁爱自由
7楼-- · 2019-01-14 21:11

Any Static member can be accessed by Directly class name as TestClass1.a no need of instance for it

  System.out.println(TestClass1 .a);

output: 10

查看更多
登录 后发表回答