It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




9年前关闭。




我今天在我们的代码中遇到了这个错误,花了一段时间才弄清楚。我发现它很有趣,因此决定分享它。这是问题的简化版本:
public class Test {

    static
    {
      text = "Hello";
    }

    public static String getTest() {
      return text + " World";
    }

    private static String text = null;
}

猜猜Test.getTest();返回什么,为什么?

最佳答案

它应该打印“null world”。静态初始化按照列出的顺序进行。如果将声明移到比静态块高的位置,则应显示“Hello World”。

07-24 09:45
查看更多