IIB

  • The initializer Block is used to initialize instance data members. 
  • IIB’s are executed when an object is created.
  • The number of times we created the object same number of time IIB will be called
  • IIB is used to initialize all the instance variables in one place and that gives us better readability of code.
  • Hierarchy of Execution———->IIB->Constructor->Main.

Flow Diagram of IIB

Example of IIB

package javaBasics.iibandsib;

public class iibExample
{

    //     The code in'{ }' denotes IIB
    {
        String s="/* Instance Initialization Block */";
        System.out.println("This is"+" "+s);
    }

    {
        String s1="/* Multiple IIB's are allowed in java */";
        System.out.println("This is"+" "+s1);
    }
//    {
//        static String s1="/* we cannot call static members also */";
//    }
    public static void main(String[] args)
    {
//      IIB is called immediately after the object is created
        iibExample ex=new iibExample();
        System.out.println("Calling IIB from Main");

    }

}

Output

This is /* Instance Initialization Block */
This is /* Multiple Instance Initialization Block */
Calling IIB from Main

Accessing static and non static members using IIB

package javaBasics.iibandsib;

public class iibExample2
{
    String s1="This is non-static global variable";
    static String s2="This is static global variable";
    {
        System.out.println(s1);
        System.out.println(s2);
        System.out.println("This is IIB which is used to call static and non-static variables");
    }

    public static void main(String[] args)
    {
        iibExample2 ex=new iibExample2();
        System.out.println("Calling a IIB"+" "+"From main");
    }
}

Output

This is non-static global variable
This is static global variable
This is IIB which is can be used to call static and non-static variables
Calling a IIB From main

SIB

  • A static block in a program is a set of statements that are executed by the JVM (Java Virtual Machine) before the main method.
  • If we want to perform any task we can define that task inside the static block, this task will be executed at the time of class loading before the main method.
  • The static keyword is used for the management of memory mainly.
  • static keyword can be used with variables, methods, blocks, and nested classes.
  • Any number of static blocks can be defined, and these static blocks will be executed from top to bottom.
  • SIB runs before the main method and it doesn’t require an invoking statement.
  • SIB Flow Diagram
  • Syntax
static
{
     **********
     **********
     // statements….
     **********
     **********
}

Example of SIB

package javaBasics.iibandsib;

public class sibExample1
{
    static
    {
        System.out.println("This Static Initialization Block");
    }

    public static void main(String[] args)
    {
//      Object invoking is not necessary to invoke SIB
        System.out.println("From the Main method");
    }
}

Output

This Static Initialization Block
From the Main method

Accessing static members and using multiple SIB

package javaBasics.iibandsib;

public class sibExample2
{
    int i=10;
    static String a="Accessing the static global variables from SIB";
    static
    {
        System.out.println(a);
//  non-static variable i(int i) cannot be referenced from a static context
//  System.out.println(i);

        System.out.println("SIB 1");

    }
    static
    {
//  Multiple SIB
        System.out.println("SIB 2");
    }

    public static void main(String[] args)
    {
        System.out.println("This is from the main ");
    }
}

Output

Accessing the static global variables from SIB
SIB 1
SIB 2
This is from the main