2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Calling static method even if reference contain null object

Here is my program,

package com.test.code;

class C {
    static void callMe() {
        System.out.println("I am in static method");

public class MyClass {
    public static void main(String[] args) {
        C c = null;

And when I compile and run the program its success with the following output.

I am in static method

But still unable to understand how can the program execute successfully even if c is referring to a null object.


And here the explanation what you are expecting,

  • There is no need for an instance while invoking static member or method.Since static members belongs to class rather than instance.
  • Even though c refer to null, a NullPointerException is not thrown. The result 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 method to access (because the method callMe is static).
Answer is