CodesJava

Easy learning with example program codes

Instance Initializer block in java


Instance initializer block:

Instance initializer block is a mechanism provided by java compiler to define a group of statements common to all constructors at a single place. At the compilation time, compiler moves these statements at the beginning of all constructors after super. It is can also be used to initialize the instance variable.

Example:

AnonymousBlockExample1.java

/**
 * This program is used to show the use of AnonymousBlock.
 * @author CodesJava
 */
class Display {
      int a, b;
 
      //Anonymous or instance initializer Block
      {
          System.out.println("AnonumousBlock called.");
          a = 10;
      }
 
     //default constructor
     Display(){
        System.out.println("default constructor called.");
     }
 
     //one argument constructor
     Display(int num){
        System.out.println("one parameter constructor called.");
        b = num;
     }
 
     //method to display values
     public void display(){
            System.out.println("a = " + a);
            System.out.println("b = " + b);
     }
}
 
public class AnonymousBlockExample1 {
       public static void main(String args[]){
              Display obj1 = new Display();
              obj1.display();
 
              Display obj2 = new Display(20);
              obj2.display();
       }
}

Output:

AnonumousBlock called.
default constructor called.
a = 10
b = 0
AnonumousBlock called.
one parameter constructor called.
a = 10
b = 20

Download this example.

If two Anonymous Blocks are used then they will execute in the same order in which they are appear.

Example:

AnonymousBlockExample2.java

/**
 * This program is used to show that if two AnonymousBlocks
 * are used then they will execute in the same order in
 * which they are appear.
 * @author CodesJava
 */
class Display {
      int a, b, c;
 
      //First Anonymous or instance initializer Block
      {
         System.out.println("First AnonumousBlock called.");
         a = 10;
      }
 
      //Second Anonymous or instance initializer Block
      {
          System.out.println("Second AnonumousBlock called.");
          b = 20;
      }
 
      //default constructor
      Display(){
          System.out.println("default constructor called.");
      }
 
      //one argument constructor
      Display(int num){
              System.out.println("one parameter constructor called.");
              c = num;
     }
 
     //method to display values
     public void display(){
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            System.out.println("c = " + c);
     }
}
 
public class AnonymousBlockExample2 {
       public static void main(String args[]){
              Display obj1 = new Display();
              obj1.display();
 
              Display obj2 = new Display(30);
              obj2.display();
       }
}

Output:

First AnonumousBlock called.
Second AnonumousBlock called.
default constructor called.
a = 10
b = 20
c = 0
First AnonumousBlock called.
Second AnonumousBlock called.
one parameter constructor called.
a = 10
b = 20
c = 30

Download this example.

If static and non-static Anonymous Blocks are used then static Anonymous Block is executed only once.

Example:

AnonymousBlockExample3.java

/**
 * This program is used to show that if static and non-static
 * AnonymousBlocks are used then static AnonymousBlocks is
 * executed only once.
 * @author CodesJava
 */
class Display {
      int a, b;
 
      //static Anonymous or instance initializer Block
      static {
              System.out.println("Static AnonumousBlock called.");
      }
 
      //non-static Anonymous or instance initializer Block
      {
          System.out.println("Non-Static AnonumousBlock called.");
          a = 20;
      }
 
      //default constructor
      Display(){
          System.out.println("default constructor called.");
      }
 
      //one argument constructor
      Display(int num){
         System.out.println("one parameter constructor called.");
         b = num;
      }
 
      //method to display values
      public void display(){
             System.out.println("a = " + a);
             System.out.println("b = " + b);
     }
}
 
public class AnonymousBlockExample3 {
       public static void main(String args[]){
              Display obj1 = new Display();
              obj1.display();
 
              Display obj2 = new Display(30);
              obj2.display();
      }
}

Output:

Static AnonumousBlock called.
Non-Static AnonumousBlock called.
default constructor called.
a = 20
b = 0
Non-Static AnonumousBlock called.
one parameter constructor called.
a = 20
b = 30

Download this example.

In which order static initializer  block, instance initialize  block, super and constructor are called?

static initialize  block – super- instance initialize  block – constructor.

Example:

AnonymousBlockExample4.java

/**
 * This program is used to show that in which order static
 * AnonumousBlocks, non-static AnonumousBlocks, super and
 * default constructors are called.
 * @author CodesJava
 */
class Show{
      Show(){
             System.out.println("Super class constructor.");
      }
}
 
class Display extends Show{
      //static Anonymous or instance initializer Block
      static {
              System.out.println("Static AnonumousBlock called.");
      }
 
      //non-static Anonymous or instance initializer Block
      {
        System.out.println("Non-Static AnonumousBlock called.");
      }
 
      //default constructor
      Display(){
           super();
           System.out.println("default constructor called.");
      }
}
 
public class AnonymousBlockExample4 {
       public static void main(String args[]){
               Display obj = new Display();
       }
}

Output:

Static AnonumousBlock called.
Super class constructor.
Non-Static AnonumousBlock called.
default constructor called.

Download this example.
 
Next Topic: super in java with example.
Previous Topic: this in java with example.

Related Topics:

Package in java with example.
Access modifier in java with example.
Static import in java with example.
Package class in java with example.
this in java with example.
Instance initialize/ Anonymous block in java with example.
super in java with example.
Static in java with example.
final in java with example.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap