-1

Following example code is from : http://javarevisited.blogspot.in/2011/11/static-keyword-method-variable-java.html

public class TradingSystem1 {

    private static String category = "electronic trading system";
    public static void main(String[] args) {
        TradingSystem1 system = null;
        System.out.println(system.category);
    }

}

It prints "electronic trading system" instead of NullPointerException. Can anybody explain why? How can category be referenced on a null object- system?

a Learner
  • 4,944
  • 10
  • 53
  • 89

6 Answers6

3

JLS - Example 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:

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

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

Long Story Short: Runtime is smart enough to know that the field is not actually required for accessing the static properties and so it surprises.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
1

The "category" field is in static context, then not need any instance of TradingSystem1 for access.

The correct access to "category" is:

TradingSystem1.category
fjtorres
  • 266
  • 6
  • 13
1

Static fields are not tied to any instance:

    TradingSystem1 system = null;
    System.out.println(system.category);

is the same as

    System.out.println(TradingSystem1 .category);
sergeyan
  • 1,173
  • 1
  • 14
  • 28
0

It prints "electronic trading system" instead of NullPointerException. Can anybody explain why? How can category be referenced on a null object- system?

category is a static variable and static variables are intialized when the class is loaded. So the memory allocation and initialization for variable category is done when the class is loaded and hence you are able to fetch its value. static variables can be fetched using the instance or the class name but internally it will be referenced using the class name in the compiled code.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0
private static String category = "electronic trading system";

category is a static string and we know that for calling static string we need to create objects

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

You are referring to a class variable, a static member. Class variables do not exist per instance, but only once per object (See Chapter 8.3.1.1 (static Fields).

The Java Language Specification specifies in Chapter 15.11.1 (Field Access Using a Primary):

If the field is static: The Primary expression is evaluated, and the result is discarded. If evaluation of the Primary expression completes abruptly, the field access expression completes abruptly for the same reason. If the field is final, then the result is the value of the specified class variable in the class or interface that is the type of the Primary expression. If the field is not final, then the result is a variable, namely, the specified class variable in the class that is the type of the Primary expression.

So, for static fields, the value of the primary expression (in your example system) is discarded. The null value is simply ignored, because the compiler only needs to know the type (the class) in order to access the class variable (the static member).

h2stein
  • 546
  • 3
  • 13