使用空对象引用调用静态方法时会发生什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它已被编译器优化掉,只是因为不需要该类的实例。编译器基本上替换
为
通常,您自己这样做也是一个很好的做法。即使是普通的 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
by
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.
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();
java 语言规范规定引用 get 会被评估,然后被丢弃,然后调用静态方法。
15.12.4.1。计算目标引用(如果需要)
这与使用
this
引用调用静态方法时的行为相同。this
被评估然后被丢弃,然后调用该方法。规范中甚至有一个与您的示例类似的示例。
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.
嗯,这完全没问题。静态方法不会被类 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.
是的我们可以。
仅当我们使用 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...