0

I do not understand why the output for the following code. Confusion resides in the main method. I cannot see why "in main" is not printed first, even tough it is the first line to be read.

    class StaticSuper {

    static {
        System.out.println("super Static block");
    }

    StaticSuper() {

        System.out.println("SUper COntrductor");
    }
}

class StaticTest extends StaticSuper {

    static int rand;

    static {
        rand = (int) (Math.random() * 6);
        System.out.println("statick block  " + rand);
    }

    StaticTest() {

        System.out.println("constructor");
    }

    public static void main(String[] args) {

        System.out.println("in main");
        StaticTest ts = new StaticTest();

    }

}
eneko
  • 333
  • 4
  • 14
  • 1
    the difference between this example and the question this duplicates is that here the main method is part of a class with static initializers, loading the class causes the initializers to run. The main method cannot be entered until the class it's part of has loaded. – Nathan Hughes Apr 14 '15 at 13:20
  • 1
    to clarify, the rules listed in this [answer](http://stackoverflow.com/a/2007701/217324) should explain the order in which your output appears. – Nathan Hughes Apr 14 '15 at 13:33

0 Answers0