在java中调用静态方法 [英] calling static method in java

查看:34
本文介绍了在java中调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何调用(静态) 空引用上的方法不会抛出 NullPointerException?

谁能解释为什么下面程序的输出是Called"

Can any one explain why the output of the following program is "Called"

public class Test4{

  public static void method(){
    System.out.println("Called");
  }

  public static void main(String[] args){
    Test4 t4 = null;
    t4.method();
  }
}

我知道我们可以使用类引用调用静态方法,但在这里我使用空引用调用.请澄清我的疑问

I know we can call static method with class reference , but here I am calling using null reference . please clarify my doubt

推荐答案

在字节码中

Test4 t4 = null;
t4.method();

将会

Test4 t4 = null;
Test4.method();

编译器会将调用转换为静态方法的类名.参考我自己的问题已经问过了.

Compiler would convert the call with the class name for static methods. refer to this question on SO which i myself have asked it.

这篇关于在java中调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆