使用空对象引用调用静态方法时会发生什么?

发布于 2024-08-20 12:05:31 字数 267 浏览 7 评论 0原文

public class CallingStaticMethod {
public static void method() {
    System.out.println("I am in method");
}
public static void main(String[] args) {
    CallingStaticMethod csm = null;
    csm.method();
   }
}

有人可以解释一下上面代码中如何调用静态方法吗?

public class CallingStaticMethod {
public static void method() {
    System.out.println("I am in method");
}
public static void main(String[] args) {
    CallingStaticMethod csm = null;
    csm.method();
   }
}

Can someone explain how the static method is invoked in the above code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌伤ぢ 2024-08-27 12:05:31

它已被编译器优化掉,只是因为不需要该类的实例。编译器基本上替换

csm.method();

CallingStaticMethod.method();

通常,您自己这样做也是一个很好的做法。即使是普通的 IDE 也会警告您通过实例访问静态方法,至少 Eclipse 在这里是这样做的。

It's been optimized away by the compiler, simply because having an instance of the class is not necessary. The compiler basically replaces

csm.method();

by

CallingStaticMethod.method();

It's in general also a good practice to do so yourself. Even the average IDE would warn you about accessing static methods through an instance, at least Eclipse does here.

梦里人 2024-08-27 12:05:31

Java 允许您使用 Class 实例来调用静态方法,但您不应混淆此允许,就好像该方法将在用于调用它的实例上调用一样。

实例.方法();

相同;

与Class.method()

Java allows you to use a Class instance to call static methods, but you should not confuse this allowance as if the method would be called on the instance used to call it.

instance.method();

is the same as

Class.method();

请叫√我孤独 2024-08-27 12:05:31

java 语言规范规定引用 get 会被评估,然后被丢弃,然后调用静态方法。
15.12.4.1。计算目标引用(如果需要)

这与使用 this 引用调用静态方法时的行为相同。 this 被评估然后被丢弃,然后调用该方法。

规范中甚至有一个与您的示例类似的示例。

当目标引用被计算然后由于调用模式是静态而被丢弃时,不会检查该引用是否为 null:

class Test1 {
    static void mountain() {
        System.out.println("Monadnock");
    }
    static Test1 favorite(){
        System.out.print("Mount ");
        return null;
    }
    public static void main(String[] args) {
        favorite().mountain();
    }
}

The java language specification says the reference get's evaluated, then discarded, and then the static method gets invoked.
15.12.4.1. Compute Target Reference (If Necessary)

This is the same behavior when you use the this reference to call a static method. this gets evaluated then discarded then the method is called.

There is even an example in the specification similar to your example.

When a target reference is computed and then discarded because the invocation mode is static, the reference is not examined to see whether it is null:

class Test1 {
    static void mountain() {
        System.out.println("Monadnock");
    }
    static Test1 favorite(){
        System.out.print("Mount ");
        return null;
    }
    public static void main(String[] args) {
        favorite().mountain();
    }
}
檐上三寸雪 2024-08-27 12:05:31

嗯,这完全没问题。静态方法不会被类 A 的对象实例访问。无论您通过类名还是引用来调用它,编译器都会通过类 java.lang.Class 的实例来调用它。

仅供参考,java 中的每个类(上图中的 CallingStaticMethod)都是“java.lang.Class”类的一个实例。每当定义一个类时,实例都会被创建为 java.lang.Class CallingStaticMethod = new java.lang.Class();

因此该方法是在“CallingStaticMethod”上调用的,因此不会发生空指针异常。

希望这有帮助。

Well this is perfectly ok. The static method is not being accessed by the object instance of class A. Either you call it by the class name or the reference, the compiler will call it through an instance of the class java.lang.Class.

FYI, each class(CallingStaticMethod in the above illustration) in java is an instance of the class 'java.lang.Class'. And whenever you define a class, the instance is created as java.lang.Class CallingStaticMethod = new java.lang.Class();

So the method is called on 'CallingStaticMethod ' and so null pointer exception will not occur.

Hope this helps.

所有深爱都是秘密 2024-08-27 12:05:31

是的我们可以。
仅当我们使用 null 对象调用非静态方法时,它才会抛出 NullPointerException 。如果方法是静态的,它将运行&如果方法是非静态的,它将通过NPE...

要了解更多信息,请单击此处...

Yes we can.
It will throw NullPointerException only if we are calling non static method with null object. If method is static it will run & if method is non static it will through an NPE...

To know more click here...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文