[Java] The behavior of the JVM before executing the main method

Source: Internet
Author: User

When the JVM executes a Java program, it starts executing the code from the main method of a specified Java class, and passes an array of strings as arguments to the main method. For example, on a Unix system, execute the following command

Java Test reboot Bob Dot Endzo

The JVM invokes the main method of Test, passing four strings "reboot", "Bob", "Dot" "Endzo" as arguments to the main method.

So, what does the JVM need to do before executing the main method? This article will simply answer this question.

The JVM initiates a call to the main method, which passes through four phases: load (load), link (Linkage), initialize (initialization), call (invocation)

Load a class of Test

At the very beginning, there was no test class in the JVM, and, to be precise, there was no binary tab code for the test class, which required loading test first. The JVM uses the ClassLoader (class loader) to search for the binary code of Test and then load. Throws an exception if the classloader cannot be searched.

Link a class Test

After the JVM contains the binary code of the test class, it is necessary to connect the test binary form to the runtime state of the JVM through a link action before the test binary code can be executed. The link contains three actions: Validate (Verify), prepare (Prepare), Parse (Resolve), where the parsing behavior is optional.

Verify: Check that the loaded test class binary code is in the correct format and that the semantics are in accordance with the Java language specification and the JVM specification.

Preparation: Allocating static storage space, as well as the data structures required by any JVM, such as a list of methods (method table)

Parse: Examine the class or interface referenced by the test class, load other referenced classes or interfaces, and check if they are correct.

Initializes a class of Test

Initialization includes initialization of static variables, and initialization of static code blocks. initialization is done in text order. if the Test class can be initialized, its immediate parent class needs to be initialized first, and if the immediate parent class can be initialized, the immediate parent class of the immediate parent class needs to be initialized first to be recursive. The object class has no parent class, so the object class is initialized after the recursion ends.

When initializing the immediate parent class, if the immediate parent class has not yet been loaded, you need to load, link, and initialize the immediate parent class first. Therefore, during the initialization of a class stage, it is possible to throw a load error, link error, initialization error.

Call Test.main

The JVM invokes the main method of Test when the previous load, link, and initialization are finished properly. The called main must be decorated as public, static, void. There are two types of acceptable forms:

Public staitc void Mian (string[] args)

Public staitc void Main (String ... args)

  

Resources

12.1 java Virtual Machine Startup, the Java Language specification, Java SE 8 Edition

[Java] The behavior of the JVM before executing the main method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.