Instance initialization block In Java
Instance initialization block in Java is the only block of Java which do not have any name. whenever we want to perform some common task in each constructor then put the code of that task into the Instance initialization block. For example: database connection.
There are two types of Instance initialization block In Java:
1. init Block
2. static block
Init Block
1. init block is used to initialize the instance data member. init block is always executed before any constructor whenever that constructor is used for creating a new object.
2. we can define instance data member inside the init block and it was made for this purpose only but there are a lot of operations we can perform in an init block.
3. All those operations which we want to complete before a constructor executed, we can write them in instance initialization block.
POINTS TO REMEMBER:
· init block is always executed before any constructor whenever that constructor is used for creating a new object.
· We can have more than one init block in a single class.
· All the block will be executed together before constructor.
· We can define instance data member inside the init block.
· The code of init block is inserted into the first line of each constructor at run time whenever a constructor is used for making a new object.
Question: Why init block concept implemented in Java?
Answer: It is because of requirement to perform some common/same task in every constructor.
Example of single Instance initialization block In Java program
class Demo { //init block defined { System.out.println("Init Block Defined"); } Demo() { System.out.println("normal constructor"); } Demo(int x) { System.out.println(x + "constructor overloading"); } public static void main(String... s) { new Demo(); System.out.println(); new Demo(20); System.out.println(); new Demo(); System.out.println(); } } |
OUTPUT:
Init Block Defined
In this program, the init block invoked every time a constructor is called.
Example of More than one Instance initialization block In Java program
class Initblock2 { //first init block { System.out.println("Init Block 1"); } Initblock2() { System.out.println("normal constructor"ond init block { System.out.println("Initblock 2 : That was fine"); } Initblock2(int x) { System.out.println(x + "constructor overloading"); } //third init block { System.out.println("Init Block 3 : wew this is working"); } public static void main(String... s) { new Initblock2(); System.out.println(); new Initblock2(20); System.out.println(); new Initblock2(); System.out.println(); } } |
Output:
Init Block 1
Initblock 2 : That was fine
Init Block 3 : wew this is working
normal constructor
Init Block 1
Initblock 2 : That was fine
Init Block 3 : wew this is working
20 constructor overloading
Init Block 1
Initblock 2 : That was fine
Init Block 3 : wew this is working
normal constructor
In this program(with more than one init block), we can see that all the init blocks are executed before the constructors.
Static initialization block In Java
A static initialization block is used to dynamically initialize the value of static data members. A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
POINTS TO REMEMBER
1. Static initialization block is used to initialize the static data members dynamically
2. Static block is always executed at class loading time
3. Static block is always executed only once in the life cycle of the class.
Example of static block:
class Temp { public static int x; static { try { x=System.in.read(); //to read a line from keyboard } catch(Exception e) {} }} class Temp1 { public static void main(String[] args) { System.out.println(Temp.x); //accessing static data member }} class Temp2 { public static void main(String... s) { System.out.println(Temp.x); //accessing static data member } } |
· In this program, we made two class withmain() method inside them.
· We defined a static variable x at class level. But via static block, we will get different values of x each time the program executes.
· So the need was something which can be run at class loading time. and we achieved this through static blocks. So static block made to dynamically allot the value to static data member.
In this program, the init block invoked every time a constructor is called.
Example of More than one Instance initialization block In Java program
class Initblock2
{
//first init block
{
System.out.println("Init Block 1");
}
Initblock2()
{
System.out.println("normal constructor");
}
//second init block
{
System.out.println("Initblock 2 : That was fine");
}
Initblock2(int x)
{
System.out.println(x + "constructor overloading");
}
//third init block
{
System.out.println("Init Block 3 : wew this is working");
}
public static void main(String... s)
{
new Initblock2();
System.out.println();
new Initblock2(20);
System.out.println();
new Initblock2();
System.out.println();
}
}
Output:
In this program(with more than one init block), we can see that all the init blocks are executed before the constructors.
Static initialization block In Java
A static initialization block is used to dynamically initialize the value of static data members. A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
POINTS TO REMEMBER
1. Static initialization block is used to initialize the static data members dynamically
2. Static block is always executed at class loading time
3. Static block is always executed only once in the life cycle of the class.
Example of static block:
class Temp
{
public static int x;
static
{
try
{
x=System.in.read(); //to read a line from keyboard
}
catch(Exception e)
{}
}}
class Temp1
{
public static void main(String[] args)
{
System.out.println(Temp.x); //accessing static data member
}}
class Temp2
{
public static void main(String... s)
{
System.out.println(Temp.x); //accessing static data member
}
}
· In this program, we made two class withmain() method inside them.
· We defined a static variable x at class level. But via static block, we will get different values of x each time the program executes.
· So the need was something which can be run at class loading time. and we achieved this through static blocks. So static block made to dynamically allot the value to static data member.
No comments:
Post a Comment