`

java编译器的聪明

 
阅读更多

代码 如下 :

 

public class TestStatic
{
    
    public static void main(String[] args)
    {
        TestStatic1 test = null;
        System.out.println(test.ss);
    }
}

class TestStatic1
{
    static String ss = "1223";
    
    Set set=new HashSet();
}

 想象中的结果:报控制正异常。

 实际结果 :打印出1223.

 

   反编译之后的结果 :

import java.io.PrintStream;

public class TestStatic
{
  public static void main(String[] args)
  {
    TestStatic1 test = null;
    System.out.println(TestStatic1.ss);
  }
}
 

  编译器自动对这种不合理的方式进行了优化。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics