In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about the impact of the Final keyword on the JVM class loader, the content of the article is of high quality, so the editor will share it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
When a class has a variable declared as static final, and such a variable has an impact on the class loader, first take a look at the following example.
Package com.bird.classLoad; class FinalTest {public static final int a = 6 System.out.println 3; static {System.out.println ("FinalTest static block");}} public class Test3 {public static void main (String [] args) {System.out.println (FinalTest.a);}}
Because an is a static final variable and it equals 6 FinalTest 3, its value is known at compile time, so direct access to the value of a does not cause initialization of the class. As a manifestation, the static static code will not be loaded quickly.
The running result is
two
Looking at an example.
Package com.bird.classLoad; import java.util.Random; class FinalTest4 {public static final int a = new Random (). NextInt; static {System.out.println ("FinalTest4 static block");} public class Test4 {public static void main (String [] args) {System.out.println (FinalTest4.a);}}
This static final variable a can't be known until run time because I cannot know its exact value at compile time, so accessing FinalTest4.an itself will cause the initialization of the FinalTest4 class. That is, static static code loads quickly.
The running result is
FinalTest4 static block 82
The following example shows that when a subclass is actively accessed, it causes initialization of its immediate parent class.
Package com.bird.classLoad; class Parent {static int a = 3; static {System.out.println ("Parent static block");} class Child extends Parent {static int b = 4; static {System.out.println ("Chind static block") }} public class Test5 {public static void main (String [] args) {System.out.println (Child.b);}}
Because of direct access to Child,b, the Parent class is initialized first, followed by the Child class.
The running result is
Parent static block Chind static block 4
If you access the variables of the parent class directly through the subclass, only the parent class will be initialized, not the subclass
Package com.bird.classLoad; class Parent {static int a = 3; static {System.out.println ("Parent static block");}} class Child extends Parent {static {System.out.println ("Chind static block");}} public class Test5 {public static void main (String [] args) {System.out.println (Child.a) }}
If you directly access the a variable of the Parent class, only the parent class will be initialized, not the Child class.
The running result is as follows
Parent static block 3 about the impact of the Final keyword on the JVM class loader is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.