can we call any method on null object?

Deadly 提交于 2019-12-30 10:06:12

问题


is it possible?

Object obj=null;

obj.someMethod();

someMethod{/*some code here*/}

回答1:


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.




回答2:


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




回答3:


No. In Java, null is not an object.




回答4:


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




回答5:


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

[...]




回答6:


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.




回答7:


you cant execute

null.someMethod(); !!!




回答8:


This will always throw a NullPointerExcpetion unless someMethod is declared static. However, calling static methods on an instance is very bad practice.




回答9:


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.




回答10:


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.



来源:https://stackoverflow.com/questions/5855188/can-we-call-any-method-on-null-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!