在路上

导航

静态类中,静态方法和静态变量的执行顺序按出现执行

package com.study;

public class StaticCode {
    static {
        i = 33;// i's value is from 0 to 33
        g();
    }

    static int i = 1;// i's value is from 33 to 1
    static String prior = "prior";
    static String last = f() ? g() : prior;

    static boolean f() {
        return i == 1;//i's value is from 1 to 3
    }

    static String g() {
        i = 3;
        return "g";
    }

    public static void main(String[] arg) {
        System.out.println(last); //g
        System.out.println(i);  //3
    }
}

  

posted on 2018-11-18 19:02  Clarence Yang  阅读(284)  评论(0编辑  收藏  举报