Differences between Instance initialization block and Static initialization block in java - Features in detail with programs


Contents of page :
  • What are Instance initialization block & Static initialization block in java

  • Features of static initialization block in java >
  • Features of instance initialization block in java >



  • Program 1 to show instance initialization blocks executes after static initialization blocks in java>
  • Program 2 to show execution flow of static initialization blocks and instance initialization blocks when SuperClass and SubClasses are used in java >

  • Difference between static initialization and instance initialization block in java>


What are Instance initialization block & Static initialization block in java
Instance initialization block in java can be used to initialize instance variables in java.
Static initialization block in java can be used to initialize static variables in java.

Features of static initialization block in java >
  • Static blocks are also called Static initialization blocks in java.
  • Static block executes when class is loaded in java.
  • static blocks executes before instance blocks in java.
  • Only static variables can be accessed inside static block in java
  • static blocks can be used for initializing static variables or calling any static method in java.
  • this keyword cannot be used in static block in java.


Features of instance initialization block in java >
  • Instance blocks are also called instance initialization blocks in java
  • Instance block executes when instance of class is created in java.
  • Also known as non-static initialization block in java.
  • instance blocks executes after static blocks in java.
  • Static and non-static variables (instance variables) can be accessed inside instance block in java.
  • instance blocks can be used for initializing instance variables or calling any instance method in java.
  • this keyword can be used in instance block in java.


Program 1 to show instance initialization blocks executes after static initialization blocks in java>
class MyClass {
  
   /* Static block */
   static {
          System.out.println("static block");
   }
   /* Non-Static block (Instance block) */
   {
          System.out.println("instance/non-static block");
   }
   /* Constructor */
   MyClass() {
          System.out.println("MyClass constructor");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class BlockTest {
   public static void main(String[] args) {
         
          //Create instance of MyClass.
          new MyClass();
         
   }
         
}
/*OUTPUT
static block
instance/non-static block
MyClass constructor
*/


Program 2 to show execution flow of static initialization blocks and instance initialization blocks when SuperClass and SubClasses are used  in java>
/**
* SuperClass
*/
class SuperClass {
  
   /* Static block */
   static {
          System.out.println("SuperClass ----------> static block");
   }
   /* Non-Static block (Instance block) */
   {
          System.out.println("SuperClass ----------> Instance/non-static block");
   }
   /* Constructor*/
   SuperClass() {
          System.out.println("SuperClass ----------> constructor");
   }
}
/**
* SubClass
*/
class SubClass extends SuperClass {
  
   /* Static block */
   static {
          System.out.println("SubClass > static block");
   }
   /* Non-Static block (Instance block) */
   {
          System.out.println("SubClass > Instance/non-static block");
   }
   /* Constructor*/
   SubClass() {
          System.out.println("SubClass > constructor");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class BlockTest {
   public static void main(String[] args) {
         
          //Create instance of SubClass.
          new SubClass();
         
   }
         
}
/*OUTPUT
SuperClass ----------> static block
SubClass > static block
SuperClass ----------> Instance/non-static block
SuperClass ----------> constructor
SubClass > Instance/non-static block
SubClass > constructor
*/


Difference between static initialization and instance initialization block in java>
Static block
instance block
Known only as static initialization block in java.
Also known as non-static initialization block in java.
static blocks executes before instance blocks in java.
instance blocks executes after static blocks in java.
Only static variables can be accessed inside static block
Static and non-static variables (instance variables) can be accessed inside instance block.
static blocks can be used for initializing static variables
or
calling any static method in java.
instance blocks can be used for initializing instance variables
or
calling any instance method in java.
static blocks executes when class is loaded in java.
instance block executes only when instance of class is created, not called when class is loaded in java.
this keyword cannot be used in static blocks.
this keyword can be used in instance block.


Summary >
So in this core java tutorial we learned What are Instance initialization block & Static initialization block in java. Features of static initialization block in java. Features of instance initialization block in java. Program to show instance initialization blocks executes after static initialization blocks in java. Program to show execution flow of static initialization blocks and instance initialization blocks when SuperClass and SubClasses are used in java. Difference between static initialization and instance initialization block in java>


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

What are Blocks in java

Multiple catch block in java

try catch finally block in java
Finally block in java


Inner class/ nested class, static, local and anonymous inner class in java


JVM (java virtual machine) in detail in java

How Garbage Collection (GC) works internally in detail in java - BEST EXPLANATION EVER

Towers of Hanoi problem with n disks in java


No comments:

Post a Comment