心血来潮的一个有关问题,java运行顺序
心血来潮的一个问题,java运行顺序
interface DeclareStuff {
public static final int EASY = 3;
void doStuff(int t); }
public class test implements DeclareStuff {
public static void main(String [] args) {
int x = 5;
new test().doStuff(++x);
System.out.println("x "+x);
}
public void doStuff(int s) {
s += EASY + ++s;
System.out.println("s " + s);
}
}
运行 结果:
s 16
x 6
为什么先运行doStuff方法 而不是先从main运行。
------解决思路----------------------
main方法中 先执行了 new test().doStuff(++x); 打印了 s 16
再执行了 System.out.println("x "+x); 打印了 x 6
interface DeclareStuff {
public static final int EASY = 3;
void doStuff(int t); }
public class test implements DeclareStuff {
public static void main(String [] args) {
int x = 5;
new test().doStuff(++x);
System.out.println("x "+x);
}
public void doStuff(int s) {
s += EASY + ++s;
System.out.println("s " + s);
}
}
运行 结果:
s 16
x 6
为什么先运行doStuff方法 而不是先从main运行。
------解决思路----------------------
main方法中 先执行了 new test().doStuff(++x); 打印了 s 16
再执行了 System.out.println("x "+x); 打印了 x 6