One of the most basic question in core java that we encounter in interviews is related to blocks/static blocks.
I am sharing a problem that will clear the basic concepts on How Blocks and Static Blocks Work in Java ?
Yes, we can define blocks like -
Note: It does not give any compile time error.
As we can see in the screenshot the order of execution of blocks and constructor in Java is -
I am sharing a problem that will clear the basic concepts on How Blocks and Static Blocks Work in Java ?
Sample code displaying block, static block and constructor execution. |
{
System.out.println("Starting block....");
}
Note: It does not give any compile time error.
As we can see in the screenshot the order of execution of blocks and constructor in Java is -
- Static block is the first block that gets loaded.
- Then any other blocks as defined
- Constructor is last to be loaded
No comments:
Post a Comment