Javatpoint Logo
Javatpoint Logo

Illustrate Class Loading and Static Blocks in Java Inheritance

In Java, class loading is an essential method that happens while a category is accessed or referenced in a software. When a Java program begins, the Java Virtual Machine (JVM) locates and loads the important classes into the memory which includes locating the bytecode for the class, verifying its correctness, and allocating memory for it.

Java inheritance permits instructions to inherit fields and methods from their parent class. When a subclass is loaded then the JVM additionally masses it discern elegance to make sure all required lessons are available for execution.

Static blocks play a substantial position for the duration of the class loading manner. A static block is a block of code enclosed in the "static" keyword and is executed only once whilst the class is loaded into memory. It is normally used for initializing static variables or performing extra setup responsibilities.

Regarding Java inheritance, static blocks are executed in a specific order. Initially, the static blocks of the parent class are executed, followed by the static blocks of the subclass. This ensures that static variables are initialized and setup tasks occur in the correct sequence.

In static block inheritance, it is vital to keep in mind the subsequent rules:

  1. Static blocks are executed in the order they appear in the code hierarchy:
    • When a class is loaded. All the static blocks in that class are completed first and, in the order, they are described.
    • If the class is a superclass, then the static blocks of the superclass are carried out inside the equal order.
    • It guarantees that static blocks are completed before every other code within the class.
  2. Static blocks are executed only once per class:
    • Static blocks are carried out while the class is loaded into the memory and they may be finished only as soon as in keeping with class, regardless of how many instances of the class are created.
    • Subsequent instances of the class do no longer cause the execution of static blocks again.

FileName: MainLoading.java

Output:

Parent class first static block
Parent class second static block
Child class static block

Explanation:

In the above code, the Parent class contains two static blocks one is first static block and the other is second static block. The Child class still has a single static block.

When program starts execution, the static blocks are executed during the class loading process in the following order:

Step 1: The JVM loads the Parent class as it is referenced for the first time.

Step 2: The JVM encounters the first static block in the Parent class and executes it, resulting in the output "Parent class first static block."

Step 3: Then, the JVM encounters the second static block in the Parent class and executes it, resulting in the output "Parent class second static block."

Step 4: Next, the JVM loads the Child class.

Step 5: The JVM encounters the static block in the Child class and executes it, resulting in the output "Child class static block."

Step 6: Finally, the program continues with the creation of an instance of the Child class.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA