Blocks in Java

Blocks in Java

Blocks are self executable.

Block has no name.

Block is used to perform some callback before initialized any thing.

Blocks are define inside class.

There are two types of blocks:

  • Static Block
  • Non-Static block

1. Static Block

  • A static block define with static keyword that is called static block.
  •     class A{
    
            static{
                //static block
            }
    
        }
        

  • A static block execute when your class is loaded on JVM.
  •     class A{
    
            static{
                System.out.println("static block");
            }
    
            public static void main(String [] h){
    
            }
    
        }
            
        //save and compile
        //javac A.java
    
        //run by
        //java A
    
        Output is : static block
        

  • A single class can have multiple static blocks.
  •     class A{
    
            static{
                System.out.println("first static block");
            }
    
            static{
                System.out.println("second static block");
            }
    
        }
        

  • A static block execution order same as well as they are define.
  •     class A{
    
            static{
                System.out.println("first static block");
            }
    
            static{
                System.out.println("second static block");
            }
    
            public static void main(String [] h){
    
            }
    
        }
    
        Output is : first static block
        Output is : second static block    
        

    2. Non-Static Block

  • It is also called init block.
  • A block define without static keyword that is called non static block.
  •     class A{
        
            {
            //non-static block
            }
        
        }
        

  • A non static block executes when jvm create an object.
  •     class A{
        
            {
                System.out.println("non-static block");
            }
        
            public static void main(String [] h){
                new A();
            }
        
        }
        
        Output is : non-static block
        

  • A single class can have a multiple non static block.
  •     class A{
    
            {
                System.out.println("first non-static block");
            }
    
            {
                System.out.println("second non-static block");
            }
    
        }    
    
        

  • A non static block execution order same as well as they are define.
  •     class A{
        
            {
                System.out.println("first non-static block");
            }
        
            {
                System.out.println("second non-static block");
            }
        
            public static void main(String [] h){
                new A();
            }
        }    
        
        Output is : first non-static block
        Output is : second non-static block
        

    Some Examples :


        class A{
            static int i=10;
    
            static{
                System.out.println(i);
            }
    
            public static void main(String [] h){
             
            }
        }
    
        Output is : 10
        

        class A{
    
            static{
                System.out.println(i);
            }
    
            static int i=10;
    
            public static void main(String [] h){
             
            }
        }
    
        Output is : Compile time error 
        /*In case of block variable must be 
        declare first then use from any block*/
        

        class A{
    
            int i=10;
    
            static{
                System.out.println(i);
            }
    
            public static void main(String [] h){
             
            }
        }
    
        Output is : Compile time error 
        

        class A{
    
            int i=10;
    
            {
                System.out.println(i);
            }
    
            public static void main(String [] h){
            new A(); 
            }
        }
    
        Output is : 10
        

        class A{
    
            static int i=10;
    
            {
                System.out.println(i);
            }
    
            public static void main(String [] h){
             new A();
            }
        }
    
        Output is : 10 
        

        class A{
    
            {
                System.out.println(i);
            }
    
            static int i=10;
    
            public static void main(String [] h){
            new A(); 
            }
        }
    
        Output is : 10 
        

    No comments: