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 mainly shows you "Java program initialization order is how", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Java program initialization order is how" this article.
The initialization of Java programs generally follows three principles (decreasing priorities in turn):
1. Static objects (variables) take precedence over non-static objects (variables), where static objects (variables) are initialized only once, while non-static objects (variables) may be initialized many times.
2, the parent class takes precedence over the child class for initialization.
3, initialize according to the order in which the member variables are defined. Even if variable definitions are scattered among method definitions, they are initialized before any method is called.
Java program initialization can be done in many different code blocks (such as static code blocks, constructors, etc.), which are executed in the following order:
Parent static variable
Parent static code block
Subclass static variable
Subclass static code block
Parent class non-static variable
Parent class non-static code block
Parent class constructor
Subclass non-static variable
Subclass non-static code block
Subclass constructor.
Here is an example of the order in which different modules are initialized:
Class Base {static {System.out.println ("Base static block");} public Base () {System.out.println ("Base constructor");} public class Derived extends Base {static {System.out.println ("Drevied static block") } {System.out.println ("Drevied block");} public Derived () {System.out.println ("Drevied constructor");} public static void main (String args []) {new Derived () }}
First, make your own judgment using the execution order given above:
The running result of the program is:
Base static block
Derived static block
Base block
Base constructor
Derived block
Derived constructor
So, is the running result consistent with yours?
Finally, let's look at the real question of the interview:
What is the result of running the following code?
Class B extends Object {static {System.out.println ("Load B1");} public B () {System.out.println ("Create B");} static {System.out.println ("Load B2") }} class An extends B {static {System.out.println ("Load A");} public A () {System.out.println ("Create A") }} public class Testclass {public static void main (String [] args) {new A ();}}
The result of the operation is:
Load B1 LoadB2 Load A Create B Create A
The question of the execution order that appears in the interview is nothing more than changing the above code into several forms, but it remains the same, as long as you master the law of its execution order, all the problems can be easily solved.
The above is all the contents of this article "what is the initialization sequence of Java programs?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.