Sunday, 1 November 2015

WHAT IS A STATIC BLOCK AND WHEN TO USE STATIC BLOCK IN JAVA..??




  INTRODUCTION :

       Hey folks.. welcome back ..In this post im going to tell you why and when we must execute static blocks..

  WHAT IS A STATIC BLOCK..??

       Hmm… as I said in the earlier posts, our main method is the boss and it will get executed first by the compiler.. !!! A company can have not only a BOSS but also a CEO right..? Answer is obviously yes..

      One day.. CEO of the company fought with the compiler that “Not all the time the boss can be given first priority but I just also want to be given some priority and executed first by you.. “ is what the CEO said to the compiler

    The complier thought for a while and just accepted to what the CEO said..

    Then the rule came into being that..

    “EVEN THE CEO OF THE COMPANY WILL BE GIVEN THE FIRST PRIORITY AND WILL BE EXECUTED EVEN BEFORE THE BOSS”

     The CEO is nothing but STATIC BLOCK and the boss is nothing but main method..
 
WHEN TO USE STATIC BLOCK..?

     So,If we want to execute any code even before the main method, we can write the code inside the static block and make it execute.

     Static block will get executed before main method

      CAN A CLASS HAVE ANY NUMBER OF STATIC BLOCK..?

      Yes.. a class can have any number of static block and each block will get executed in the same order they are written.

   CONSIDER THE FOLLOWING EXAMPLE..

       Class classname
    {
          static
             {
                System.out.println(“Static block is executed”);
             }
          public static void main(String args[])
            {
              System.out.println(“Main method is accessed”);
             }
   }
         OUTPUT:

         Static block is executed
         Main method is accessed

   EXPLANATION:

          As I said before, Static block is executed first even before the main method and so the above outputs are printed.

       A class can have any number of static blocks .For example

      Class classname
   {
          static
            {
               System.out.println(“1st Static block is executed”);
            }
         static
         {
           System.out.println(“2nd Static block is executed”);
         }
         static
          {
            System.out.println(“3rd Static block is executed”);
          }
      public static void main(String args[])
          {
             System.out.println(“Main method is accessed”);
           }
  } 

   OUTPUT : 

    1st Static block is executed
   2nd Static block is executed
   3rd Static block is executed
   Main method is accessed

EXPLANATION:

       Note in the above program, static blocks are executed in the order in which they are present and then only main method is executed.

   CAN STATIC METHODS BE PRESENT INSIDE THE STATIC BLOCKS??

        Yes.. Static methods can be inside the static blocks and they can be called and executed.

    Example:

   static
      {
          System.out.println(1st Static block is executed”);
          Staticmethod();
     }

   static
     {
       System.out.println(“2nd Static block is executed”);
     }

    Static void Staticmethod()
     {
        System.out.println(“static method is called”);
     }
   public static void main(String args[])
     {
         System.out.println(“main method is executed”);
      }

OUTPUT:

    1st Static block is executed
    static method is called
    2nd Static block is executed
     main method is executed

 EXPLANATION:

       Initially 1st static block will get executed and then static method is called..from the static block and then second static block is executed and then main method is executed.
So this is what the static blocks are.. !! If  you want anything to get executed even before the main method then use static blocks

  CONCLUSION:

      So that’s it for this post.. raise your collars and stay happy as you have understood one more important concept in java.. !! Hope you understood the concepts better..!! Place your valuable feedbacks below so that will help me posts more!! Hit like button and share with your friends..!! Have a pleasant day :)







No comments :

Post a Comment