Java 程序的执行从哪一行开始?是主要方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15863530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:05:39  来源:igfitidea点击:

Execution of a Java Program starts from which line? Is it main method?

javamainexecution

提问by user2096592

From where does execution of a java program starts? I heard people say its from main method. I think execution starts from static block. Am i right??

java程序的执行从哪里开始?我听到人们说它来自 main 方法。我认为执行从静态块开始。我对吗??

回答by A.H.

The output of this programm:

这个程序的输出:

public class XXX {

    static YYY dependend = new YYY();

    static {
        System.out.println("3");
    }

    public static void main(String[] args) {
        System.out.println("4");
    }
}

class YYY {
    static {
        System.out.println("1");
    }
    YYY(){
        System.out.println("2");
    }
}

is, of course

当然是

1
2
3
4

So: The entry pointfor your program is main. But there is code executed before that. And there is no need that this "executed before main" code is in the same class. And there is also no need that this code is in a static initializer (see "2").

所以:你的程序的入口点main. 但是在此之前执行了代码。并且不需要这个“在 main 之前执行”代码在同一个类中。并且也不需要此代码在静态初始化程序中(参见“2”)。

回答by Avi

The main method is the entry point to your program. If the class that contains the "main" method has static members that need to be initialized or a static code block, this will be executed BEFORE the "main" method.

main 方法是程序的入口点。如果包含“main”方法的类有需要初始化的静态成员或静态代码块,这将在“main”方法之前执行。

Look at this sample code:

看看这个示例代码:

public class Test {

    private static Object obj = new Object();

    public static void main(String[] args) {

        System.out.println("test");
    }

}

If you put a breakpoint in the object initialization line you will see it runs before the println line.

如果在对象初始化行中放置一个断点,您将看到它在 println 行之前运行。

回答by TheEwook

This is the start method of a java program:

这是一个java程序的启动方法:

public static void main(String[] args){
    ...
}

回答by zch

The static blocks are executed when the class is initialized. For the class containing mainmethod it will be before calling this method, because class has to be initialized before any of it's method is used. For other classes it can be later or never, if the class doesn't need to be initialized.

静态块在类初始化时执行。对于包含main方法的类,它将在调用此方法之前进行,因为在使用它的任何方法之前必须初始化类。对于其他类,如果不需要初始化该类,它可以稍后或永远不会。

回答by Matt3o12

A Java application will be normally initialised by the main method:

Java 应用程序通常由 main 方法初始化:

public static void main(String... args){
    System.out.println("Executing my application...");
}

The static block will be executed when the JVM loads the class. You cannot start your application without a main method or the JVM will appear an error message.
It is theoretically possible to execute your code with a static block (example). But it is a bad way to initialise your application because the doSomethingElsemethod might be called by an other script, which doesn't want to create a gui (or whatever you do in your initialisation method). For example:

JVM 加载类时将执行静态块。如果没有 main 方法,您将无法启动应用程序,否则 JVM 将出现错误消息。
理论上可以使用静态块(示例)执行您的代码。但这是初始化应用程序的一种糟糕方式,因为该doSomethingElse方法可能会被其他脚本调用,而该脚本不想创建 gui(或您在初始化方法中所做的任何事情)。例如:

class Test2 extends Object{
    public static void doSomething(){
        System.out.println("Calling Test's doSomethingElse method.");
        Test.doSomethingElse();
    }
}

Test2's doSomething method wants only to call doSomethingElsebut the method which creates the GUI is also called because the class Testis loaded by the JVM. When the JVM loads a class - and the class has got a static block - the static block will be called at fist. Test's static block calls now the executingClassmethod and the GUI will be created (but Test2 wants only to call doSomethingElse.

Test2 的 doSomething 方法只想调用doSomethingElse但创建 GUI 的方法也被调用,因为该类Test是由 JVM 加载的。当 JVM 加载一个类 - 并且该类有一个静态块 - 将首先调用静态块。Test 的静态块现在调用该executingClass方法并创建 GUI(但 Test2 只想调用doSomethingElse.

Finally, you shouldn't use a static block to initialise your application:

最后,您不应该使用静态块来初始化您的应用程序:

  1. The static block should only be used for creating stuff which is needed by that class.
  2. mainmethod is also needed.
  3. It might have unpredictable effects.
  1. 静态块应该只用于创建该类需要的东西。
  2. main方法也是需要的。
  3. 它可能会产生不可预测的影响。

回答by Yogesh Kumar

If a class contains static block and main method () jvm Wil executive the static block first and then main method will be executed. But if you don't mention the static block in a class and the class contain only main method then jvm starts execution from main method only.

如果一个类包含静态块和 main 方法 () jvm 将先执行静态块,然后执行 main 方法。但是如果你没有在类中提到静态块并且该类只包含 main 方法,那么 jvm 只从 main 方法开始执行。