(静态)变量和(静态)代码块的也是有执行顺序的,与代码书写的顺序一致。在(静态)代码块中可以使用(静态)变量,但是被使用的(静态)变量必须在(静态)代码块前面声明。
public static int MIN = min(A, B); 放在静态代码块之前输出为0,放在之后输出为赋值后比较的最小值。
public class Solution {
public static int A;
public static int B;
static {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
A = Integer.parseInt(reader.readLine());
B = Integer.parseInt(reader.readLine());
reader.close();
}
catch (IOException e){
e.printStackTrace();
}
}
public static int MIN = min(A, B);
public static void main(String[] args) {
System.out.println(MIN);
}
public static int min(int a, int b) {
return a < b ? a : b;
}
}