LLII of the Valley
What happens when you start a Java program can be summarised in four steps (LLII). Loading, Linking, Initialization, Invoke Main. (Ch5 JVM spec covers this in detail).
- The bootstrap class loader loads the initial class.
- JVM links the class, initializes the class and invokes the main method.
What does loading involve?
Loading involves locating the binary form of a class or interface type (usually the class file format, and constructing a Class object to represent the class or interface).
The loading process is defined by the class ClassLoader and its subclasses. If an error occurs at the loading stage, a LinkageError will be thrown. An example LinkageError would be a NoClassDefFoundError.
What does linking involving?
"Linking a class or interface involves verifying and preparing that class or interface, its direct superclass, its direct superinterfaces, and its element type (if it is an array type)" (JVMSpec)
Verifying a class means ensuring its binary representation is structurally valid. A VerifyError is thrown if structural constraints on JVM code are violated. Preparing a class means intialising the static fields to their default values. This does not result in execution of any Java machine code.
What does intializing involve?
Initializing involves init'ing static fields in the class, and any static initializers (procedural code declared in a static {} scope). There may be other methods that need initialising for example to support Reflection API.
What does executing main involve?
Running the linked program!
No comments:
Post a Comment