IIB(Instance Initialisation Block)

 
Instance initialization block code runs right after the call to super() in a constructor, in other words, after all super constructors have run.
The order in which initialization blocks appear in a class matters.
If a class has more than one they all run in the order they appear in the class file.
 
Some rules to remember
Initialization blocks execute in the order they appear.
Static Initialization blocks run once when the class is first loaded.
Instance Initialization blocks run every time a class instance is created.
Instance Initialization blocks run after the constructor’s call to super()
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class IIB1
    {
        IIB1(int  i)
            {
                System.out.println("constructor");
            }
            {
                System.out.println("IIB");
            }
        public  static void main(String[] args)
            {
                IIB1  g1 = new IIB1(10);
                System.out.println("---------");
                IIB1  g2 = new IIB1(20);
                System.out.println("---------");
                IIB1  g3 = new IIB1(30);
                System.out.println("---------");
            }
    }

O/P:

NOTE:

Both IIB and Constructor are only called when you create your object.
IIB called before the constructor when you create your object. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class OneOrMoreIIB
    {
        {
            System.out.println("IIB1");
        }
    OneOrMoreIIB()
        {
            System.out.println("constructor");
        }
    {
        System.out.println("IIB2");
    }
public  static void main(String[] args)
    {
        OneOrMoreIIB  i1 = new OneOrMoreIIB();
        System.out.println("---------");
        OneOrMoreIIB  i2 = new OneOrMoreIIB();
        System.out.println("---------");
        OneOrMoreIIB  i3 = new OneOrMoreIIB();
        System.out.println("---------");
    }
   }
                    

O/P:

NOTE:

No matter whether the IIB is within constructor block or out side but it is called before constructor when you create your object.
If there are more than one IIB then they get executed sequentially.