freetimestudys@gmail.com          +91-7097633380
  Login Here                        

instance blocks:-

1) The instance blocks are executed irrespective of any condition.
2) The instance blocks and instance variables are executed before constructor execution. If you are giving a chance to the constructors then only instance blocks are executed.
3) In the class it is possible to take the any number of instance blocks. the execution order is top to bottom.
4) Instance blocks are executed based on the object creation. If we are creating ten objects ten times instance blocks will be executed.
5) If the source file contains inheritance concept at that situation first parent class instance block will be executed then child class instance blocks will be executed.

instance blocks program

class sup
{
{
System.out.println("instance block 1");
}
{
System.out.println("instance block 2");
}
public static void main(String[] args)
{
sup s=new sup();

}
}

intput and output

output:instance block 1 instance block 2

The instance variables and instance blocks are having same priority at that situation the execution order is top to botton.

class sup
{
int i=m1();
int m1()
{
System.out.println("m-1 instance method");
return 10;
}
{
System.out.println("instance Block");
}
public static void main(String[] args)
{
sup t=new sup();
System.out.println(t.i);
}
}

intput and output

output:m-1 instance method instance Block 10