Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am not getting NullPointerException? [duplicate]

Possible Duplicate:
Static fields on a null reference in Java

I understand that static methods are on class level. So I am aware that I do not need to create instance to call static methods. But I am also aware that I can call static method LIKE an instance method. This is where I am confused, because I was expecting a NullPointerException while calling the static method from the null object (as in calling instance method). I would really appreciate some explanation on why I was wrong to expect a NullPointerException here.

Here is the sample code:

public class SampleClass {

    public static int getSumStatic(int x, int y){
        return x+y;
    }

    public int getDifferenceInstance(int x, int y){
        return x-y;
    }
}

public class TestClass {

    public static void main (String[] args){        
    SampleClass sc=null;

    System.out.println(SampleClass.getSumStatic(2, 2)); //as expected

    //I was expecting NullPointerException in the next line, since I am accessing null object
    System.out.println(sc.getSumStatic(4,5)); //static method , executes perfectly  

    System.out.println(sc.getDifferenceInstance(6,4));//throws NullPointerException
    }
}
like image 662
Jimmy Avatar asked Sep 30 '12 21:09

Jimmy


People also ask

How do I fix NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What could be the reason if you are getting NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

Can I catch NullPointerException?

It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.

Is NullPointerException checked or unchecked?

Answer: NullPointerException is not a checked exception. It is a descendant of RuntimeException and is unchecked.


1 Answers

Calling a static method through an instance does not require the instance to be there. As long as the compiler is able to determine the type of the variable, it makes the equivalent call statically after evaluating the sc expression and discarding the result:

System.out.println(SampleClass.getSumStatic(4,5));

From the Java Language Specification:

Section 15.12.1

❖ If the form is Primary.NonWildTypeArgumentsopt Identifier, then the name of the method is the Identifier. Let T be the type of the Primary expression. The class or interface to be searched is T if T is a class or interface type, or the upper bound of T if T is a type variable.

Section 15.12.4.1:

  1. If the second production for MethodInvocation, which includes a Primary, is involved, then there are two subcases:

❖ If the invocation mode is static, then there is no target reference. The expression Primary is evaluated, but the result is then discarded.

like image 72
Sergey Kalinichenko Avatar answered Sep 21 '22 14:09

Sergey Kalinichenko