Showing posts with label Static Method. Show all posts
Showing posts with label Static Method. Show all posts

Wednesday 9 July 2014

What happens when a static method is invoked using a null object reference

No we can't.

It will throw NullPointerException because null is not an object in java. If method is static it will run & if method is non static it will through an NPEnull does not represent some "base" object, it represents a reference which does not point to any object at all.

For Example:-
class TestClass {
    static void myMethod() {
        System.out.println("Hello World...");
    }
}

class Test {
    public static void main(String[] args) {
        TestClass tc = null;        tc.myMethod();    }
}

Above code will print "Hello World..." as myMethod() is static method.