1
class Order {
  Order() {
    System.out.println("Cat");
  }

  public static void main(String... Args) {
    Order obj = new Order();
    System.out.println("Ant");
  }

  static {
    System.out.println("Dog");
  }

  {
    System.out.println("Man");
  }
}

Output is

Dog
Man
Cat
Ant

I know that static block will get executed at first. While creating an object for 'Order' class, usually default constructor is called(as it is there). I want to know how it is possible that a nameless block that is executed before the the execution of constructor.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243

0 Answers0