Different output after execution main class

后端 未结 1 1077
[愿得一人]
[愿得一人] 2021-01-18 21:23
public class Main {
    public static void main(String[] args){
        System.out.println(X.Y.Z);
    }
}

class X {
    static class Y {
        static String Z =          


        
相关标签:
1条回答
  • 2021-01-18 21:43

    This is Java Puzzler #68 - it should always print Result 2. Quote:

    It turns out that there is a rule that governs program behavior under these circumstances. When a variable and a type have the same name and both are in scope, the variable name takes precedence [JLS 6.5.2]. The variable name is said to obscure the type name [JLS 6.3.2]. Similarly, variable and type names can obscure package names. This rule is indeed obscure, and any program that depends on it is likely to confuse its readers.

    0 讨论(0)
提交回复
热议问题