Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.293 :

Posted: Fri Apr 29, 2011 5:23 pm
by ETS User
Although the answer remains the same the class is actually first referenced when executing the class file with the java command so if the code was changed to :

Code: Select all

public class StaticTest
{
   static
   {
      System.out.println("In static");
   }
   {
      System.out.println("In non - static");
   }
   public static void main(String args[ ])
   {
       System.out.println(" 1 ");
       StaticTest st1 = new StaticTest();            //2
       System.out.println(" 2 ");
       StaticTest st2 = new StaticTest(); //3
   }
}
You would get the same answer. Line //1 does not actually cause the static init blocks to run as they were already run in order to execute the main method is called.

Re: About Question com.enthuware.ets.scjp.v6.2.293 :

Posted: Fri Apr 29, 2011 8:31 pm
by admin
You are right. Explanation is updated to reflect this fact.

Thank you for your feedback!
Paul.