0

When i coded this:

public class Animal {
     {
      System.out.println("In instance block");
     }
     static
     {
      System.out.println("In Static block");
     }
      public Animal(){
      System.out.println("In Default constructor");
     }
     public static void main(String[] args) {
      Animal a = new Animal();
     }
    }

The ouput is :

In Static block
In instance block
In Default constructor

The Anonymous block shown in the output is Banging my head.......Please Help me in understanding the output

Usama Zafar
  • 77
  • 1
  • 3
  • And your question is? – akortex Jul 04 '18 at 16:04
  • The anonymous block is added to the start of every constructor. – Peter Lawrey Jul 04 '18 at 16:14
  • static blocks are executed at the time the class is loaded, which implies, executed exactly once throughout the runtime of your program, and before you have the chance to build any instance of the class. Nonstatic anonymous blocks are commons to all constructors. They're executed before the constructor, whichever it is. If there are more than one, they are executed in the order they appear in the code. After anonymous blocks are executed, then the constructor is executed. – kumesana Jul 04 '18 at 16:19

0 Answers0