Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test.cert;
- /*
- * author: Nick Humphrey, http://nickhumphreyit.blogspot.no/
- * date: 20131126
- *
- * java certification practice exam question about runtime initialization process:
- * http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=303&p_certName=SQ1Z0_803
- *
- * JVM 7 runtime initialization process:
- * http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4
- *
- *
- * NOTE: i ran this file with debug breakpoints on all lines of code to find the processing order and here's what i found:
- *
- * access modifiers for variables and methods (private, public, protected, etc) don't seem to affect processing order, ALTHOUGH the "main" method must have the "public" modifier
- *
- * standard getter and setter methods for variables don't get processed
- *
- * "static final" variables don't get processed by the JVM during initialization or instantiation of the class, i.e. debugger never stops on their breakpoints
- *
- *
- * processing order:
- * 1. static variables and static initialization blocks, in textual order of appearance
- * 2. "main" method:
- * 2.1 begin executing any logic encountered
- * 2.1.1 if a call to instantiate the containing class is made (i.e. "new JVMClassInitializationProcessingOrderTest()"), temporarily pause execution of "main" method logic
- * 2.1.1.1 initialize (non-static) instance variables and (non-static) initialization blocks, in textual order of appearance
- * 2.1.1.2 process the class' constructor
- * 2.1.1.3 resume execution of the remaining logic in the "main" method
- */
- class JVMClassInitializationProcessingOrderTest {
- // "static final" variable
- static final String staticFinalVar = "staticFinalVar"; // breakpoint, processing order: not processed
- // "static" method, called internally during initialization of "static" variable "staticVar2"
- static String initStaticVar2() {
- return "staticVar2"; // breakpoint, processing order: 3
- }
- // "final" instance variable
- final String finalInstVar1 = "finalInstVar1"; // breakpoint, processing order: 7
- // initializer block: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
- {
- System.out.println("init block"); // breakpoint, processing order: 8
- }
- // instance variable
- String instVar1 = "instVar1"; // breakpoint, processing order: 9
- // "static" variable
- static String staticVar1 = "staticVar1"; // breakpoint, processing order: 1
- // "static initializer block": http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
- static {
- System.out.println("static init block 1"); // breakpoint, processing order: 2
- }
- // class constructor
- JVMClassInitializationProcessingOrderTest() {
- System.out.println("constructor"); // breakpoint, processing order: 10
- }
- // instance method, called internally in "main" method
- void go() {
- System.out.println("go"); // breakpoint, processing order: 11
- }
- // "static" variable
- static String staticVar2 = initStaticVar2(); // breakpoint, processing order: 4
- // "main" method
- public static void main(String[] args) {
- new JVMClassInitializationProcessingOrderTest().go(); // breakpoint, processing order: 6
- System.out.println(JVMClassInitializationProcessingOrderTest.staticFinalVar); // breakpoint, processing order: 12
- }
- // "static initializer block": http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
- static {
- System.out.println("static init block 2"); // breakpoint, processing order: 5
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement