Lakhyadeep Sen’s Post

While continuing with static concepts in Java, static blocks were another interesting feature to understand. They are used when some initialization needs to happen at the class level before objects are created. Things that became clear : • a static block runs when the class is loaded into memory • it executes before the main method • it is commonly used for class level initialization • static blocks run only once, regardless of how many objects are created • they are often used when static variables need some setup logic A small example helped understand the execution order : class Demo { static { System.out.println("Static block executed"); } public static void main(String[] args) { System.out.println("Main method executed"); } } In this case, the static block runs first when the class loads, and then the main method executes. Seeing this execution flow made the class loading process in Java a little clearer. #java #oop #programming #learning #dsajourney

To view or add a comment, sign in

Explore content categories