Thursday, 2 July 2015

Blocks in JAVA

BLOCK IN JAVA

JAVA supports 2 types of blocks.
1. Static block
2. Non static block

If the block is declared within the static keyword then the block is known as static block.
The static block always executes before the main method, which is executed by the JVM.

If the block is declared without any static keyword then it is a Non Static block.
Non static block always executes before the constructor called by the user.
Static block is executed only for a single time for the whole life cycle of the program. Whereas non static block may execute "n" number of times, as it depends on the user
The existence of the main method will be checked by the JVM before the execution of the Static block.

Static block execution depends on the JVM. Non static block execution depends on the User.

You can write more than 1 static or non static blocks within a single class, as it is accessed top to bottom manner.