为什么在定义之前调用方法?

时间:2017-07-09 10:03:47

标签: java compilation

public class MainClass {

    public static void main(String[] args) {
        anotherTest();
        test();
    }
    public static void test() {
        System.out.println("Printed from the test method.");
    }
    public static void anotherTest() {
        System.out.println("Printed from the anotherTest method.");
    }
}

我想知道为什么你可以在定义上面的代码之前调用一个方法。上面的代码在页面底部显示了两个方法,但是在加载之前我在main方法中调用它们。如果我在Python中做类似的事情,我会收到错误。

1 个答案:

答案 0 :(得分:0)

这是因为Python在运行时被编译,而在java中,编译器处理这个问题(它在整个代码中搜索声明)

相关问题