代码1:
public class test {
static {System.out.println("I am here in static");}
public test()
{
System.out.println("I am here in constructor");
}
public static void main(String[] args)
{
System.out.println("I am here in Main");
test staticCheck=new test();
}
输出 1: 我在这里静态 我在主这里 我在构造函数中
代码2:
public class test {
{System.out.println("I am here in static");}
public test()
{
System.out.println("I am here in constructor");
}
public static void main(String[] args)
{
System.out.println("I am here in Main");
test staticCheck=new test();
}
}
输出 2: 我在主这里 我在这里静态 我在构造函数中
最佳答案
关于java - 谁能解释为什么这两个代码都按此顺序打印输出的原因(静态关键字执行优先级),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66794047/