我们可以在 Java 中使用 null 对象调用静态方法吗?如果是这样,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24800309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we call a static method with a null object in Java? If so, how?
提问by vermaraj
Since static methods can be called directly from the class (i.e. ClassName.methodName), why it is required to call a static method with the object of the class?
既然静态方法可以直接从类中调用(即ClassName.methodName),那为什么还需要用类的对象来调用静态方法呢?
If someone knows then, elaborate with example.
如果有人知道,请举例说明。
public static void methodA(){
}
采纳答案by nosid
The following code contains an example, in which a static method is called via a null
reference.
以下代码包含一个示例,其中通过null
引用调用静态方法。
public class Test {
public static void main(String... args) {
Test test = null;
test.greeting(); // call with null reference
}
public static void greeting() {
System.out.println("Hello World");
}
}
Because Test::greeting
is a static method, the expression test.greeting()
is identical to Test.greeting()
. For that reason, there is no NullPointerException
thrown at runtime.
因为Test::greeting
是静态方法,所以表达式test.greeting()
与Test.greeting()
. 出于这个原因,NullPointerException
在运行时没有抛出。
回答by Suresh Atta
There is no need for an instance while invoking static member or method.
调用静态成员或方法时不需要实例。
Since static members belongs to class rather than instance.
由于静态成员属于类而不是实例。
Example 15.11.1-2. Receiver Variable Is Irrelevant For static Field Access
Example 15.11.1-2. Receiver Variable Is Irrelevant For static Field Access
The following program demonstrates that a null reference may be used to access a class (static) variable without causing an exception:
以下程序演示了可以使用空引用访问类(静态)变量而不会导致异常:
The example from spec it self.
来自规范本身的示例。
class Test3 {
static String mountain = "Chocorua";
static Test3 favorite(){
System.out.print("Mount ");
return null;
}
public static void main(String[] args) {
System.out.println(favorite().mountain);
}
}
And the analysis of why it is happening
以及为什么会发生的分析
Even though the result of favorite() is null, a NullPointerException is not thrown. That "Mount " 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 field to access (because the field mountain is static).
即使 favorite() 的结果为 null,也不会抛出 NullPointerException。打印的“Mount”表明 Primary 表达式确实在运行时被完全评估,尽管事实上只有它的类型而不是它的值用于确定要访问的字段(因为字段 Mountain 是静态的)。
回答by Vincent Lal
Yes,and this is a strange situation. Below is the example.
是的,这是一个奇怪的情况。下面是示例。
public class Test{
public static String foo(){
System.out.println("foo");
return "";
}
public static void main(String args[]){
Test t=null;
t.foo();//no error will be thrown
}
}
Output: foo
输出: foo
Explanation : it is expected this should throw a null pointer exception but it just gives you a warning that "The static method foo() from the type Test should be accessed in a static way". But when executing it will work.
解释:预计这应该抛出一个空指针异常,但它只是给你一个警告“应该以静态方式访问来自类型 Test 的静态方法 foo()”。但是在执行时它会起作用。
回答by Dixit Singla
Very well you can call a static method with null object.
很好,您可以使用空对象调用静态方法。
See the example below.
请参阅下面的示例。
public class Hashing {
public static void Hash() {
System.out.println("hello");
}
public static void main(String[] args) {
Hashing h = null;
h.Hash();
}
}
Above code snippet will print hello
上面的代码片段将打印你好
Because at the compile time h.hash()
will be converted to Hashing.hash()
since hash()
is a staticmethod.
因为在编译时h.hash()
会转换成Hashing.hash()
因为hash()
是静态方法。
When I de-compiled .class
file I got this code.
当我反编译.class
文件时,我得到了这段代码。
/*
* Decompiled with CFR 0_114.
*/
import java.io.PrintStream;
public class Hashing {
public static void Hash() {
System.out.println("hello");
}
public static void main(String[] args) {
Object h = null;
Hashing.Hash();
}
}
As you can see in the above snippet h.Hash();
is converted to Hashing.Hash();
正如您在上面的代码段中看到的那样h.Hash();
转换为Hashing.Hash();
HTH!!
哈!!