Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, April 9, 2019

Static block Importance in Java

What is static block  in java ?

Static blocks will be executed at the time (during) of class loading if we want to perform any activity we have to define that inside static block.


Static block Importance in Java


E.g. At the time of java class loading, the corresponding native libraries should be loaded. Hence, we have to define this activity inside static block.

Class Test
{
      Static
     {
            System.loadLibrary(“native library path”);
     }
}
We can observe the same in java api classes. Object, Thread classes, etc..




Public class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }
}

public class Object {

    private static native void registerNatives();
    static {
        registerNatives();
    }
}

E.g.2:
  1. Load driver class – register driver manager.
  2. Drivermanager.getconnection
  3. Prepare statement
  4. Execute query
  5. Iterate result.
  6. Close rs, state, conn



After loading every database driver class, we have to register driver class with driver manager. But, inside database driver class there is a static block to perform this activity and we are not responsible explicitly.

Class DBDriver
{
    Static
   {
      // Register this driver with driver manager.
   }
}

Note: Within a class we can declare any number of static blocks, but all these static blocks will be executed from top to bottom.

Queston 1: Without writing main method is it possible to print some statement to the console?

Answer: Yes, By using static block.


public class StaticBlockEx {
   static
     {
        for (int i = 0; i < 10; i++)
            System.out.println(i);
       //System.exit(0);
     }
   public static void main(String[] args)
   {
      System.out.println("............");
   }
}
Output:
0
1
2
3
4
5
6
7
8
9
............

Public Test
{
   Static
     {
         SOP(“Hello, I can print”);
         System.exit(0); // it stops execution.
    }
}
     
Output: Hello, I can print.


Question 2: without writing main method & static block, is it possible to print some statements to console?

Answer: yes. Of course, there are multiple ways.

Way 1:

public class StaticBlockEx {
      static int i = m1(); // using static method.
      public static int m1() {
            System.out.println("hai, I can print");
            System.exit(0);
            return 10;
      }
}

Way 2:


public class StaticBlockEx {
      static StaticBlockEx blockEx = new StaticBlockEx(); // using instance method.
      {
            System.out.println("hai, I can print");
            System.exit(0);
      }
}

Way 3:


public class StaticBlockEx {
      static StaticBlockEx blockEx = new StaticBlockEx(); // using constructor
      public StaticBlockEx() {
            System.out.println("hai, I can print");
            System.exit(0);
      }
      public static void main(String[] args) {
      }
}



Note: From 1.7 version onwards, main() method is mandatory to start the program execution. Hence, from 1.7 version onwards without writing main() method it is impossible to print some statements to the console.

No comments:

Post a Comment

Please do not add any spam links in the comments section.