Illustrate Class Loading and Static Blocks in Java InheritanceIn 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:
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. Next TopicIntroduction To Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India