TYPES OF BLOCKS JVM APPROACH
WHAT ARE THE TYPES OF BLOCK S IN JAVA ?
- There are two types of initialization blocks in java :approach
- Instance initialization Block. As explained above, the block which defined inside the class directly without the static modifier is known as the initialization block.
- static block will be run only once in the execution of program . so if you want some thing that should print before the instance of that class created then put it in the static block.
ABOUT JVM ?
JVM stands for Java Virtual Machine. It provides a runtime environment for driving Java applications or code. JVM is an abstract machine that converts the Java bytecode into a machine language. It is also capable of running the programs written by programmers in other languages (compiled to the Java bytecode).
1.Static blocks
JVM approach the block one by one .we know the normal block which means main method's block. But if an interview or any other requirement to run the value without using main method block .Its possible to achieve by using Static and Instance blocks. JVM first read is there any Static block s are in the program if any block there first print or approach the static block's properties, condition .
package jvmpro;
public class Jvmpros {
static {
System.out.println("Static block in java ");
}
public static void main(String[] args) {
System.out.println("Normal block in java ");
}
}
OUTPUT:
Static block in java
Normal block in java
We can't directly approach the static block properties in another block .But it can possible to create a variable declaration as instance with static mood for visibilities for the inside of the existing static block's variable. Now can get the variable 's value an another block .
package jvmpro;
public class Jvmpros {
static int a;
static {
a=10;
System.out.println("Static block in java ");
}
public static void main(String[] args) {
System.out.println("The value of a :"+a);
System.out.println("Normal block in java ");
}
}
OUTPUT:
Static block in java
The value of a :10
Normal block in java
2. INSTANCE BLOCK
This type of block don't have any name .JVM approach this block after the approach of static block .we can write two ways to run that. if we want to perform initialize of instance variables, then we should go for Constructor, other than initialization activity if we want to perform any activity at the time of object creation then we should go for instance block.
package jvmpro;
public class Jvmpros {
{
System.out.println("Instance block in java ");
}
public static void main(String[] args) {
Jvmpros dd=new Jvmpros();
System.out.println("Normal block in java ");
}
}
OUTPUT:
Instance block in java
Normal block in java
Lets see how was the order was works line by line approach by java's jvm:
package jvmpro;
public class Jvmpros {
{
System.out.println("Instance block in java ");
}
static {
System.out.println("Static block in java ");
}
public static void main(String[] args) {
Jvmpros dd=new Jvmpros();
System.out.println("Normal block in java ");
}
}
OUTPUT:
Static block in java
Instance block in java
Normal block in java
No comments:
Post a Comment