aspose file tools*
The moose likes Java in General and the fly likes the order of Java creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "the order of Java creation" Watch "the order of Java creation" New topic
Author

the order of Java creation

donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
create a class > app.java
compile it > javac app.java
what goes on?:
JVM loads class.
Static members are initialized.
JVM goes to the main(for a app) method.
Object is declared: i.e.: App myApp;
Object creation: i.e.: myApp = new App();
New is envoked.
New operator allocates memory.
New operator calls the appropriate ctor.
Constructor creates the "object."
Constructor body is not yet executed though.
Constructor initializes all instance variables to 0,null or false.
Constructor body is now executed.
Is this right?

[ March 14, 2003: Message edited by: donald rieck ]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, you left out a few steps
Note that the JVM load classes as needed, not at the beginning.

JLS 12 Execution
12.1 Virtual Machine Start-Up
12.1.1 Load the Class Test
12.1.2 Link Test: Verify, Prepare, (Optionally) Resolve
12.1.3 Initialize Test: Execute Initializers
12.1.4 Invoke Test.main
12.2 Loading of Classes and Interfaces
12.2.1 The Loading Process
12.3 Linking of Classes and Interfaces
12.3.1 Verification of the Binary Representation
12.3.2 Preparation of a Class or Interface Type
12.3.3 Resolution of Symbolic References
12.4 Initialization of Classes and Interfaces
12.4.1 When Initialization Occurs
12.4.2 Detailed Initialization Procedure
12.4.3 Initialization: Implications for Code Generation
12.5 Creation of New Class Instances

-1
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
-2
If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
-3
This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
-4
Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
-5
Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason.
12.6 Finalization of Class Instances
12.6.1 Implementing Finalization
12.6.2 Finalizer Invocations are Not Ordered
12.7 Unloading of Classes and Interfaces
12.8 Program Exit

[ March 14, 2003: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: the order of Java creation