Advertisement
nickhumphrey

JVM 7 Class Initialization Processing Order Test

Nov 26th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package test.cert;
  2.  
  3. /*
  4.  * author: Nick Humphrey, http://nickhumphreyit.blogspot.no/
  5.  * date: 20131126
  6.  *
  7.  * java certification practice exam question about runtime initialization process:
  8.  * http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=303&p_certName=SQ1Z0_803
  9.  *
  10.  * JVM 7 runtime initialization process:
  11.  * http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4
  12.  *
  13.  *
  14.  * NOTE: i ran this file with debug breakpoints on all lines of code to find the processing order and here's what i found:
  15.  *
  16.  *      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
  17.  *
  18.  *      standard getter and setter methods for variables don't get processed
  19.  *
  20.  *      "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
  21.  *
  22.  *
  23.  * processing order:
  24.  * 1. static variables and static initialization blocks, in textual order of appearance
  25.  * 2. "main" method:
  26.  *      2.1 begin executing any logic encountered
  27.  *          2.1.1 if a call to instantiate the containing class is made (i.e. "new JVMClassInitializationProcessingOrderTest()"), temporarily pause execution of "main" method logic
  28.  *              2.1.1.1 initialize (non-static) instance variables and (non-static) initialization blocks, in textual order of appearance
  29.  *              2.1.1.2 process the class' constructor
  30.  *              2.1.1.3 resume execution of the remaining logic in the "main" method
  31.  */
  32.  
  33. class JVMClassInitializationProcessingOrderTest {
  34.     // "static final" variable
  35.     static final String staticFinalVar = "staticFinalVar"; // breakpoint, processing order: not processed
  36.  
  37.     // "static" method, called internally during initialization of "static" variable "staticVar2"
  38.     static String initStaticVar2() {
  39.         return "staticVar2"; // breakpoint, processing order: 3
  40.     }
  41.  
  42.     // "final" instance variable
  43.     final String finalInstVar1 = "finalInstVar1"; // breakpoint, processing order: 7
  44.  
  45.     // initializer block: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
  46.     {
  47.         System.out.println("init block"); // breakpoint, processing order: 8
  48.     }
  49.  
  50.     // instance variable
  51.     String instVar1 = "instVar1"; // breakpoint, processing order: 9
  52.  
  53.     // "static" variable
  54.     static String staticVar1 = "staticVar1"; // breakpoint, processing order: 1
  55.  
  56.     // "static initializer block": http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
  57.     static {
  58.         System.out.println("static init block 1"); // breakpoint, processing order: 2
  59.     }
  60.  
  61.     // class constructor
  62.     JVMClassInitializationProcessingOrderTest() {
  63.         System.out.println("constructor"); // breakpoint, processing order: 10
  64.     }
  65.  
  66.     // instance method, called internally in "main" method
  67.     void go() {
  68.         System.out.println("go"); // breakpoint, processing order: 11
  69.     }
  70.  
  71.     // "static" variable
  72.     static String staticVar2 = initStaticVar2(); // breakpoint, processing order: 4
  73.  
  74.     // "main" method
  75.     public static void main(String[] args) {
  76.         new JVMClassInitializationProcessingOrderTest().go(); // breakpoint, processing order: 6
  77.         System.out.println(JVMClassInitializationProcessingOrderTest.staticFinalVar); // breakpoint, processing order: 12
  78.     }
  79.  
  80.     // "static initializer block": http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
  81.     static {
  82.         System.out.println("static init block 2"); // breakpoint, processing order: 5
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement