Thursday, June 22, 2017

Java-static block and instance block

instance and static block
-------------------------
instace block
-------------
A block is group of statements enclosed in between opening and closing braces.

syntax
------
     {
      //statements
     }

An instance block is a block declared in a class without any name.

An instance block is automatically called whenever an object is created.

An  instnace block is used initialize instance varibles of a class.

syntax
------
class ClassName
{

   .....;
   {
     //statements
   }
   ...;
}
Program
-------
class Test
{
   int a;
 
   {
     a=20;
     System.out.println(a);
   }
   
   public void init()
   {
      a=40;
      System.out.println(a);
    }
    public static void main(String args[])
    {

       Test t=new Test();
       t.init();

   }

}

output
------
20
40

Here first instance  block is called.

second init() is called after creating object.


static block
------------
A block declared using keyword static is known as static block.


syntax
------
    static
    {
     //statements
    }

     
static block is auotmatically executed whenever a .class is loaded into memory from a hard disk.

static block is used to initialize static variables at time of class loading.

static block is also used to perform pre-initialization i,e
intializing static variables or creating objects at the time of loading class itself is known as pre initialization.


example:
-------
class Test
{
   //instance block
   {
      System.out.println("hi");
   }
   //static block
   static
   {
      System.out.println("hello");
   }
   //main
   public static void main(String args[])
   {
      System.out.println("gm");
      Test t=new Test();
      System.out.println("ga");
   
   }
}

Analysis of program execution
-----------------------------
1--static block is executed
2--prints hello
3--main is executed
4--prints gm
5--Object is created
6--instance block is called
7--prints hi
8--prints ga

output
------
hello
gm
hi
ga


what is the output?
class Test
{
   int a;
   
   static
   {
      Test t=new Test();
      t.a=10;
      System.out.println("hello"+t.a);
   }
   {
      System.out.println("hi");
   }
   public static void main(String args[])
   {
      System.out.println("gm");
      Test t=new Test();
      System.out.println("ga");
   
   }
}

Ananlysis of execution flow
---------------------------
1--static block is called
2--Test object in static block is created
3--instance blocl is called--->hi
4--instance variable is initialized-->hello10
5--main method is called
6--"gm" is printed
7--Test object in main is created
8--instance block is called->hi is printed
9--"ga" is printed


output
------
hi
hello10
gm
hi
ga

what is the output of the following code?
class Test
{
   int a;
   
   {
      Test t=new Test();
      System.out.println("hi");
   }
   public static void main(String args[])
   {
      System.out.println("gm");
      Test t=new Test();
      System.out.println("ga");
   
   }
}

output
------
gm,hi,ga
cE
RE[StatkOverFlowError]
gm,hi,ga,hi
None


what is a static block?
static block initializes -------variables
static block is excuted as soon as-----loaded into memory.
static block is used to peform---------.
-----block is always executed first in java.

No comments: