10

I came across following code :

public class TradingSystem {

    private static String category = "electronic trading system";

    public static void main(String[] args) {
        TradingSystem system = null;
        System.out.println(system.category);
}

Output : electronic trading system

I was surprised to not find a NullPointerException !

Q1. Why didn't it throw the NullPointerException ?

Q2. Or while compile time, due to category's declaration having static made it to replace the system(i.e object reference) with TradingSystem and as such essentially TradingSystem.category was called?

KNU
  • 2,560
  • 5
  • 26
  • 39
  • Also a duplicate of http://stackoverflow.com/questions/3293353/how-come-invoking-a-static-method-on-a-null-reference-doesnt-throw-nullpointe – Raedwald Nov 27 '14 at 07:57
  • @Raedwald technically, that's not exactly duplicate to this. but good to know – KNU Nov 27 '14 at 12:32

4 Answers4

7

Java allows accessing class variables (i.e. static ones) using the instance syntax. In other words, the compiler lets you write system.category, but it resolves it to TradingSystem.category, which is independent of the instance on which it is accessed.

That is why you do not get NullPointerException. However, this syntax is not readable and confusing. That is why you should get a warning and a suggestion to use TradingSystem.category instead of system.category.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

Your code is not different from following code conceptually.

public class TradingSystem {

    private static String category = "electronic trading system";

    public static void main(String[] args) {
        System.out.println(TradingSystem.category);

    }
}

Even though you seem to be using system object reference, you are actually using static value. Java allows use instances when you use static,but you should prefer above syntax so that it is clear that you are using static ones.

Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
1

You should never call static methods using class instances, nor is it ever needed. As static methods are executed at the class level, the instance is not used and thus no null pointer exception is thrown.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • I think "should never call ..." is too harsh. Why? If so, JAVA would have never allowed syntactically to allow that, but it does. why? because anything that belongs to a Class naturally belongs to its instances and how it should be called is subject to the context. at least in literal sense. – KNU Nov 26 '14 at 11:47
  • @KNU I disagree. I don't know why Java allows this way of calling static methods (my guess is to successfully compile code that it otherwise could not) but it only leads to unexpected behaviour. Can you give an example where this is beneficial? – Konrad Höffner Nov 26 '14 at 12:30
  • @Konard I am not saying it might be beneficial but it might be more meaningful/practical to use reference. e.g. Clients need not know if the method is static/non-static while using them as Static members can be called from both the static & non-static context. – KNU Nov 26 '14 at 12:35
  • more on "why Java allows this way of calling " because think in real world anything that is known to template must also be implicitly known to the object created using that template. isn't it.Hence, IMO, Java devs have deliberately allowed it. – KNU Nov 26 '14 at 12:38
1

static is said to be "OF CLASS" not for object of a class. So here

System.out.println(system.category); "system is acting as TradingSystem"

which is right. As you do not need instantiated object to evoke static marked field or method.