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.
class FooObject {
public static void saySomething() {
System.out.println("Hi there!");
}
}
class Main {
public static void main(String[] args) {
FooObject foo = null;
foo.saySomething();
}
}
But just to make it clear - no, you can't call an instance method with a null pointer. Protecting the programmer against this is one of the really basic protections that set languages like Java apart from "lower level languages" such as C++. It enables the error to be reported at the calling end, instead of it causing an inexplicable segfault/whatnot inside the method itself.
"obj" variable references to no instance on the heap. For that reason you'll get a nullpointerexception at runtime. Even your Java IDE (Eclipse, IDEA) will catch the problem and warns you about this.
No, there is no way to call a method on a null reference (unless the method is static!). (null does not represent some "base" object, it represents a reference which does not point to any object at all.)
15.12.4.4 Locate Method to Invoke The strategy for method lookup depends
on the invocation mode.
[...]
If the invocation mode is static, no target reference is needed and overriding is not allowed. Method m of class T is the one to be invoked.
Otherwise, an instance method is to be
invoked and there is a target
reference. If the target reference is
null, a NullPointerException is thrown
at this point. Otherwise, the target
reference is said to refer to a target
object and will be used as the value
of the keyword this in the invoked
method.
A null object does not exist. In your example, you have a variable (a pointer) that can either store a reference to an instance or nothing.
If it doesn't point to an instance - well, then we can't use it to call methods or access fields.
wait, wait - this compiles and runs:
Math m = null;
System.out.println(m.max(1,2));
We can call static methods and access static fields in any variable (we just have to ignore compiler/IDE warnings!) But that is something different, a static method/variable is not called/accessed on the instance but on the class itself.
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.
But just to make it clear - no, you can't call an instance method with a null pointer. Protecting the programmer against this is one of the really basic protections that set languages like Java apart from "lower level languages" such as C++. It enables the error to be reported at the calling end, instead of it causing an inexplicable segfault/whatnot inside the method itself.
"obj" variable references to no instance on the heap. For that reason you'll get a nullpointerexception at runtime. Even your Java IDE (Eclipse, IDEA) will catch the problem and warns you about this.
No, there is no way to call a method on a
null
reference (unless the method is static!). (null
does not represent some "base" object, it represents a reference which does not point to any object at all.)This works fine however (ideone.com link):
Relevant quote from the JLS:
This won't compile as Object doesn't have
someMethod()
. However if you are talking about something likethe answer depends on whether someMethod is static or not. If it is static, the value is ignored and it doesn't matter if its null or not.
e.g.
runs fine without an exception.
you cant execute
null.someMethod(); !!!
A null object does not exist. In your example, you have a variable (a pointer) that can either store a reference to an instance or nothing.
If it doesn't point to an instance - well, then we can't use it to call methods or access fields.
wait, wait - this compiles and runs:
We can call static methods and access static fields in any variable (we just have to ignore compiler/IDE warnings!) But that is something different, a static method/variable is not called/accessed on the instance but on the class itself.