In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Java class initialization example analysis". In daily operation, I believe many people have doubts about Java class initialization example analysis problems. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java class initialization example analysis". Next, please follow the editor to study!
A little bit of eye contact
1 the system initializes this class or interface in the following six ways.
Create an instance of the class. The way to create an instance for a class includes using the new operator to create an instance, reflection to create an instance, and deserialization to create an instance. Call the static method of a class. Access or assign a value to the static property of a class or interface. Use reflection to force the creation of a java.lang.Class object corresponding to a class or interface. For example, code: Class.forName ("Person"). Initialize a subclass of a class, and when you initialize a subclass of a class, all the parent classes of that subclass are initialized. Use the java.exe command directly to run a main class, and when you run a main class, the program initializes the main class first.
A static property of type 2 final.If the property can get its value at compile time, it can be considered as a compile-time constant. When a program uses compile-time constants, the system considers this to be a passive use of the class, so it does not cause the class to initialize.
Second actual combat
1 code
Class MyTest {static {System.out.println ("static initialization block...");} / use a string direct quantity to assign the class variable of static final static final String compileConstant = "Crazy Java handout";} public class CompileConstantTest {public static void main (String [] args) {/ / access and output the compileConstant class variable System.out.println (MyTest.compileConstant) in MyTest; / / ①}}
2 run
Crazy Java handout
3 description
A static class that is decorated with final, and its value can be determined at compile time, without initializing the class.
Three actual combat
1 code
Class Tester {static {System.out.println ("static initialization block of Tester class...");}} public class ClassLoaderTest {public static void main (String [] args) throws ClassNotFoundException {ClassLoader cl = ClassLoader.getSystemClassLoader (); / / the following statement simply loads Tester class cl.loadClass ("Tester"); System.out.println ("system loads Tester class"); / / the following statement initializes Tester class Class.forName ("Tester");}}
2 run
The system loads the static initialization block of the Tester class Tester class.
3 description
LoadClass only loads the class and does not perform initialization of the class.
ForName causes classes to be forcibly initialized.
At this point, on the "Java class initialization example analysis" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.