1

Can you please shed some light on why this code produces the following output: a b c 2 3 4 1 ?
Static Initialization Block gets executed as soon as class is loaded in the JVM so I was excepting to see b first. I've seen the other similar posts but I think they do not consider the case when a static variable gets initialized with a compile-time constant.

public class InitTest{

  public InitTest(){
       s1 = sM1("1");
  }

  static String s1 = sM1("a");

  String s3 = sM1("2");

  {
     s1 = sM1("3");
  }

  static{ s1 = sM1("b"); }

  static String s2 = sM1("c");

  String s4 = sM1("4");

  public static void main(String args[]){
        InitTest it = new InitTest();
  }

  private static String sM1(String s){
       System.out.print(s);  
       return s;
  } 

}

Desaretiuss
  • 311
  • 1
  • 3
  • 12

0 Answers0