Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we call a static method with a null object in Java? If so, how?

Tags:

java

static

Since static methods can be called directly from the class (i.e. ClassName.methodName), why it is required to call a static method with the object of the class?

If someone knows then, elaborate with example.

public static void methodA(){

}
like image 973
vermaraj Avatar asked Jul 17 '14 09:07

vermaraj


People also ask

Can you call methods on null object Java?

You can call a static method on a null pointer. The pointer will naturally be completely ignored in a static method call, but it's still a case when something that (without looking at the class definition) seemingly should cause a NullPointerException runs just fine.

Can a static object be null?

That is totally expected, static initialization happens on different events which none of them occurs before your assign, so the static attribute will be null anyway.

Can we call static method using object in Java?

In Java, we cannot call the static function by using the object. It is invoked by using the class name. For example: Math.

Can a static method be called without an object?

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.


2 Answers

The following code contains an example, in which a static method is called via a null reference.

public class Test {
    public static void main(String... args) {
        Test test = null;
        test.greeting(); // call with null reference
    }
    public static void greeting() {
        System.out.println("Hello World");
    }
}

Because Test::greeting is a static method, the expression test.greeting() is identical to Test.greeting(). For that reason, there is no NullPointerException thrown at runtime.

like image 65
nosid Avatar answered Nov 15 '22 08:11

nosid


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

Since static members belongs to class rather than instance.

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:

The example from spec it self.

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);
    }
}

And the analysis of why it is happening

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

like image 20
Suresh Atta Avatar answered Nov 15 '22 08:11

Suresh Atta