编译器自动优化致使代码能够前后写的问题

public class Demo1 {
    static {
        test = 1;
//        System.out.println(test);
    }
    static int test;

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}


其实上面的代码,编译器会自动编译为:java

import java.io.PrintStream;

public class Demo1
{
  static int test = 1;

  public static void main(String[] args)
  {
    System.out.println("Hello World!");
  }
}

因此致使没有定义的,也能够先赋值。ide

注意问题:要有static的包围着,static会让编译器自动优化。
优化

相关文章
相关标签/搜索