• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

order of initialization (Java OCA 8 Programmer I Study Guide, Sybex)

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, this is an odd one. On page 202 of the OCA study guide  1z0-808 there is a section on order of initialization. It says:
1)If there is a superclass, initialize it first
2)Static variable declarations and static initializers in the order they appear in the file
3)Instance variable declarations and instance initializers in the order they appear in the file
4)The constructor


Then let's skip to the second example (not included here as my point isn't really int he code) and in the explanation it says:
Rule 1 doesn't apply. Rule 2 tells us to look at the static variable declaration and static initializers. Now that the statics are out of the way, the method main() can run...
etc

Now I wonder, where on earth does it say this in the 4 rules? Absolutely no mention of the main method. Why isn't it part of the rules then?
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • I hope you know why It says Rule 1 doesn't apply.
  • Rule 2 tells us to look at the static variable declaration and static initializers: line 4 assigns COUNT = 0 and line 5 prints 0. These two lines are executed in order they appear.

  • Why above static declaration and static initializer got executed before instance? because
    JLS 12.1.2 wrote:After class is loaded, it must be initialized before main can be invoked

    But when class is loaded? after It's first active use.
    JLS 8 12.4 wrote:Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class.

    But when initialization occurs?
    JLS 12.4.1 wrote:
    A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
    T is a class and an instance of T is created.
    A static method declared by T is invoked.
    A static field declared by T is assigned.
    A static field declared by T is used and the field is not a constant variable (§4.12.4).
    T is a top level class (§7.6) and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
    and much more ....

    Now we are done with static

  • As in first quote mentioned main is invoked after initialization of class. Now class in initialized then invokes main method means control goes in main method. If you see in OCA book on page no 19 at bottom It says We start with the main() method because that's where Java starts execution. Please also read next lines.
  • When control goes in main method first print statement is encountered so It executes It and prints "read to construct" then after that an object of InitializationOrder is created then only instance variable declaration and instance initializer gets executed in the order they appear.
  • After that constructor.

  •  
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jason Attin wrote:Now I wonder, where on earth does it say this in the 4 rules? Absolutely no mention of the main method. Why isn't it part of the rules then?

    Because the main() method is simply no part of the order in which classes (and instances) are initialized. When the main() method is executed is not part of the order of class loading/initialization, but is all about the order of how an application is executed. If you execute a class with a main() method, the JVM will first load and initialize the class containing this method (and eventually its superclasses as well), and then the main() method is executed. And classes will be loaded/initialized on an as-is needed base, depending on the code in the main() method of course.

    If you want to know which classes are loaded (and when) during the execution of an application, you can use the java command option -verbose:class. In this topic you'll see this command option shine in a code example with statics in a super- and subclass Definitely worth reading!

    Hope it helps!
    Kind regards,
    Roel

    PS. Have a cow for reading the book fully focused and with a great eye for detail. And of course creating a topic to share your doubts. Not many readers would spot this potential inconsistency between the 4 rules and the explanation of the 2nd example.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:Now class in initialized then invokes main method means control goes in main method.

    Just for clarity: Both class loading/initialization and invoking the main() method is performed by the JVM.
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Roel Thank you  , yes that is the first statement of second paragraph in Ch.12 Execution of JLS.
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks guys. @Ganesh Patekar my issue was more with why main wasn't included in the rules rather than how the four rules are applied in this case.
    Because the main() method is simply no part of the order in which classes (and instances) are initialized.
    I appreciate that. The thing is I've done java before but I had no idea that main() was executing just after the statics, that's all I was trying to say.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jason Attin wrote:The thing is I've done java before but I had no idea that main() was executing just after the statics, that's all I was trying to say.

    And at which point did you think that the main() method was executed?
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ Jason Ohh I see, I'm glad that you got what you were looking for, thank to Roel. 
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well I knew that main() was one of the first things that executes, I had no idea it came after the statics, but I knew it happened before the instance variables and before constructors
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jason Attin wrote:Well I knew that main() was one of the first things that executes, I had no idea it came after the statics, but I knew it happened before the instance variables and before constructors

    Just for clarity and to avoid misunderstanding: the main() method is executed only after the statics of the class defining this method (and possible superclasses), but before statics of other classes (which are used in the code of the main() method).
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok thanks
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In fact, allow me to expand on this, because I've just come across another question on the book that says something different and it will be useful on this thread.
    The question is on page 227 number 22.
    Here is the code:


    The problem here is in the answer (doesn't matter which one it is, but it's something they're saying in the answer):
    On line 3 of OrderDrive we refer to order for the first time. At this point the statics in Order get initalized

    I thought we just said that the statics are initialized before the main method starts, and here they're saying exactly the opposite...?
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jason Attin wrote:I thought we just said that the statics are initialized before the main method starts, and here they're saying exactly the opposite...?
    I think, statics of class are initialized before the main method starts but only on its first active use.

    Here OrderDrive is a specified class means we are asking JVM to run OrderDrive by executing command java OrderDrive so JVM do loading, linking and initialization of this OrderDrive class because It is considered as It's first active use.

    JVM also loads Order class with OrderDrive but initialization is done at It's first active use only. First active use of Order class is on line 3 of OrderDrive because a static field declared by Order is used. It is one of the 5 conditions of first active use to initialize class mentioned in JLS 12.4.1.
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If Order was super class of OrderDrive then statics of Order would have initialized first then main method of OrderDrive.
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 22102
    88
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jason Attin wrote:
    I thought we just said that the statics are initialized before the main method starts, and here they're saying exactly the opposite...?


    Perhaps, it would be better to understand the reasons, instead of applying general "rules" like "statics are initialized before blah blah blah"? ...

    Static variables (and static blocks) are initialized when the class is loaded. Classes are loaded when they are needed. And since, it is not possible to run the main() method of the class, without loading it first, hence, it is safe to say that the static variables of the class (with the main() method) is initialized before the main() method runs.

    However, in this latest example, the statics are from a different class. A class that is not needed until later, so, since there is no reason to load this secondary class, before the main() method... why would the class be loaded, and its static variables initialized, before the main() method executes?

    Henry
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK I think I understand, but I don't recall seeing this (
      the statics are from a different class. A class that is not needed until later, so, since there is no reason to load this secondary class, before the main() method... why would the class be loaded, and its static variables initialized, before the main() method executes?
    ) in the book unless I missed it of course
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:so, since there is no reason to load this secondary class, before the main() method... why would the class be loaded, and its static variables initialized, before the main() method executes?


    Actually I read that here
    Ch. 7 The Lifetime of a Type by Bill Venners, Loading wrote:
    Class loaders (bootstrap or user-defined) need not wait until a type's first active use before they load the type. Class loaders are allowed to cache binary representations of types, load types early in anticipation of eventual use, or load types together in related groups. If a class loader encounters a problem during early loading, however, it must report that problem (by throwing a subclass of LinkageError) only upon the type's first active use. In other words, if a class loader encounters a missing or malformed class file during early loading, it must wait to report that error until the class's first active use by the program. If the class is never actively used by the program, the class loader will never report the error.
    Last paragraph under Loading link Perhaps It's too old, don't know 
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jason Attin wrote:I thought we just said that the statics are initialized before the main method starts, and here they're saying exactly the opposite...?

    That's not what I have said in the post prior to your "ok thanks" post. And I made that post especially to avoid any misunderstanding, but clearly I failed miserably

    This is what I said in that post:
    Roel De Nijs wrote:Just for clarity and to avoid misunderstanding: the main() method is executed only after the statics of the class defining this method (and possible superclasses), but before statics of other classes (which are used in the code of the main() method).


    Now let's apply this statement to the code snippet from your previous postHere is what happens (between parantheses and in italics, the part of the aforementioned statement). To execute the main() method of the OrderDrive class, the JVM will first have to load/initialize the OrderDrive class (= "the statics of the class defining this method"). In order to load/initialize the OrderDrive class, the JVM needs to load/initialize its parent classes first (= "and possible superclasses"). The OrderDrive class doesn't have any superclasses defined (except for Object), so the JVM loads/initializes the OrderDrive class and thus executes its statics.
    After the OrderDrive class is loaded/initialized, the JVM starts executing the main() method (= "the main() method is executed only after the statics of the class defining this method (and possible superclasses)"). In order to be able to execute line10, the Order class needs to be loaded/initialized (= "before statics of other classes (which are used in the code of the main() method)").

    And if you use the aforementioned -verbose:class command option, that's something which can easily be confirmed. Let's try with a simple code exampleIf you compile and execute the following code using the command java -verbose:class Statics, you'll get the following output (truncated to only show the relevant parts):
    [Loaded Statics from file:/C:/home/java/testing/classes/]
    begin main
    [Loaded A from file:/C:/home/java/testing/classes/]
    [Loaded B from file:/C:/home/java/testing/classes/]
    A's Static Init
    B's Static Init
    A's Init Block
    A's Constructor
    B's Init Block
    B's Constructor
    end main


    And that's exactly as I explained above: first load Statics (class with main() method), then execute main() method, and finally load classes A and B (other classes used in main() method on an as-is needed basis).

    In fact, if you would change the main() method as followsAnd you use the same command to execute this code, this change is reflected in the output (again truncated to the relevant parts):
    [Loaded Statics from file:/C:/home/java/testing/classes/]
    begin main
    [Loaded A from file:/C:/home/java/testing/classes/]
    A's Static Init
    A's Init Block
    A's Constructor
    in main
    [Loaded B from file:/C:/home/java/testing/classes/]
    B's Static Init
    A's Init Block
    A's Constructor
    B's Init Block
    B's Constructor
    end main


    Hope it helps!
    Kind regards,
    Roel
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow! ran out of words! once again great explanation. 
    Yes comand java -verbose:class Statics cleared I was wrong about whatever I read in Ch. 7 The Lifetime of a Type by Bill Venners, Loading. Once again thank you so much
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic