1

I'm studying for Oracle's OCA 8 certification. In my studies, I came across an issue that left me with some doubts about the order of initialization of a Java object (static blocks, constructors, initialization of variables, ...). The question is as follows:

public class InitTest{
   static String s1 = sM1("a");{
      s1 = sM1("b");
   }
   static{
      s1 = sM1("c");
   }
   public static void main(String args[]){
      InitTest it = new InitTest();
   }
   private static String sM1(String s){
      System.out.println(s);  return s;
   }
}

My question is the order in which each part of the object is started:

1) {...}

2) static {...}

3) InitTest {...}

4) static String s1 = sM1("a");

Can you explain me, please?

8
  • What is not clear to you? What do you need clarification on? May 20, 2019 at 14:16
  • A side question: will a compiler error raise at point static String ...?
    – Reporter
    May 20, 2019 at 14:16
  • 1
    @reporter I don't think so, it's a valid code. May 20, 2019 at 14:18
  • 1
    Not able to find exact dupe, but take a look at the Java language specifications here, here and then here.
    – Mena
    May 20, 2019 at 14:26
  • 1
    What is no. 3 refering to? It looks like you're refering to the class itself or do you mean when the constructor is being called? In case you're refering to the class, then the order should be 3, 4, 2, 1 - the explanation is in the links to the JLS that Mena provided.
    – Thomas
    May 20, 2019 at 14:30

3 Answers 3

3

Order of initalization is always as follows:

  1. initialize superclasses recursively (not relevant for the example in question since it doesn't have a superclass)
  2. static fields and static initializers
  3. instance fields and instance initializers
  4. constructors

Hence, the order of initialization in your example will be:

1) static String s1 = sM1("a"); - static initialization blocks and static field members are the first to be processed, this happens just after the classloader loads the class (before you start using the class or create an object). If there are more initializers or static member declarations, they are executed in the order in which they are written. That's why this static field will get initialized before the static initializer block.

2) static {...} - explained in point 1. The static initializater comes before the declaration of static variable s1 so that's why it it is processed in this order (both have the same priority but here the order inside of the class wins if both have the same prio).

3) {...} - after the static initializers and static fields, the instance initializers and instance fields are initialized, so the instance initializer is next after the static initializer and static field s1. They are the first to be processed when creating a new object using a constructor and this happens before the constructor actually gets executed.

4.) InitTest {...} - The constructor gets called after everything else is initalized (all initialization blocks and field initializations).

More details about the class and object initialization order you can find in the Java Language Specification: https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.4.1, https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.4.2, https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.5

1

First: The part of s1 = sM1("b") is formatted like it is part of s1 definition, but it's completely separate.

static { ... } and static String s1 =sM1("a") are both static, which means they run when the JVM loads the class, before any of the code in your Main. they are executed in the order they are written.

{...} and InitTest{...} aren't static and they run only when you create the instance of InitTest.
{...} is initialization block and run before the constructors.

1

Anything that's static is first taken care of as there is no reason for an object availability, The possible static contents in a class are, a) static instance variable b) static code block c) static methods and are evaluated in the same order (although order for a static method doesn't matter). So in your case the s1 = SM1("a") is evaluated first which results in the call to the sM1("a") method. Next the static code block is executed which results in sM1("c") and finally the instance code block is executed with sM1("b"). If you happen to have a no arg constructor in this class, then it would have got called as the last step before the object is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.