Java: Static and Instance blocks

Static Block

static block (also called static clause) which can be used for static initializations of a class. This code inside the static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class).

Instance Block

In a Java program, operations can be performed on methods, constructors and instance blocks.IIBs are executed before constructors. They run each time when an object of the class is created.

  • Whenever class initialized and before constructors are invoked at that time instance blocks are executed.
  • They are typically placed above the constructors within braces.
  • It is not at all prime to include them in your classes.

Program