is it possible?
Object obj=null;
obj.someMethod();
someMethod{/*some code here*/}
is it possible?
Object obj=null;
obj.someMethod();
someMethod{/*some code here*/}
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.
No we can't. it will throw NullPointerException as long as the method is not static. If method is static it will run.
Read this: null : Java Glossary
No. In Java, null is not an object.
"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):
class MethodTest {
static void someMethod() {
System.out.println("Hello World");
}
}
class Test {
public static void main(String[] args) {
MethodTest mt = null;
mt.someMethod();
}
}
Relevant quote from the JLS:
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. Methodm
of classT
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
, aNullPointerException
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 keywordthis
in the invoked method.[...]
you cant execute
null.someMethod(); !!!
This will always throw a NullPointerExcpetion
unless someMethod is declared static. However, calling static methods on an instance is very bad practice.
This won't compile as Object doesn't have someMethod()
. However if you are talking about something like
MyClass o = null;
o.someMethod();
the 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.
Thread t = null;
t.yield();
runs fine without an exception.
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.
yes you can call if the method is static because static method is bound at compile time and only type of variable is used for static binding not the value of object.
if you try the same for non static method,get ready to catch a null pointer exception.