In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Java class variables and member variables initialization process", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Java class variables and member variable initialization process"!
I. initialization of the class
For the initialization of the class: the initialization of the class is generally initialized once, and the initialization of the class is mainly to initialize static member variables.
The compilation of the class determines the initialization of the class.
The class file generated by the compiler mainly makes the following changes to the classes defined in the source file:
1) declare member variables within the class in the order in which static member variables are defined.
2) initialize the member variables according to the initialization order of the original java class.
The transformation between a java class and the compiled class is as follows:
Source file:
Public class Person {public static String name= "Zhang San"; public static int age; static {age=20; System.out.println ("initialize age");} public static String address; static {address= "Beijing"; age=34;} public static void main (String [] args) {System.out.println (name); System.out.println (age) System.out.println (address);}}
When the java source code is converted into a class file, it is converted to code similar to the following:
Public class Person {public static String name; public static int age; public static String address; static {name= "Zhang San"; age=20; System.out.println ("initialize age"); address= "Beijing"; age=34;} public static void main (String [] args) {System.out.println (name); System.out.println (age) System.out.println (address);}}
The initialization order is executed according to the initialization order of the corresponding class class member variables after conversion, so all static member variables are declared first, and then assigned, and the order of assignment is also carried out according to the order in which static member variables are initialized by the source code. Note: defining a member variable and initializing it directly is equivalent to initializing it in a static code block. Are done in the order in which they are defined in the source code.
Second, the generation of objects
The initialization process for the generation of an object is similar to that of a class, but the constructor phase is added, and the source code is as follows:
Public class Person {{name= "Li Si"; age=56; System.out.println ("initialize age"); address= "Shanghai";} public String name= "Zhang San"; public int age=29; public String address= "Beijing"; public Person () {name= "Zhao Liu"; age=23; address= "Shanghai";}}
When the compiler converts to a class file, it converts to code similar to the following:
Public class Person {public String name; public int age; public String address; public Person () {name= "Li Si"; age=56; System.out.println ("initialize age"); address= "Shanghai"; name= "Zhang San"; age=29; address= "Beijing"; name= "Zhao Liu"; age=23; address= "Shanghai" }} Thank you for your reading, the above is the content of "the initialization process of Java class variables and member variables". After the study of this article, I believe you have a deeper understanding of the initialization process of Java class variables and member variables, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.