In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of what the execution order of Java static blocks, free blocks and constructors is, detailed content, clear logic, and I believe that most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Static block format:
Static
{
}
Static blocks are executed when the class is loaded, and because the parent class is preloaded when the subclass is loaded, if there is a static block in the parent class, it is executed first. The static block of the subclass is then executed immediately.
If there are multiple static blocks in a class, they are executed in the order in which they are encoded.
Note: because the class is loaded only once, each static block is executed only once.
Free block format:
{
}
Free blocks are executed when the class is instantiated.
If you want to instantiate a subclass, its constructor instantiates its parent class first. So the free block of the parent class is executed first, and then the parent class is constructed. After the constructor of the parent class is executed, the operation weight is returned to the constructor of the subclass, and the free block of the subclass begins to be executed.
Then start constructing subclasses.
The following is an example that combines parent static block, subclass static block, parent free block, subclass free block, parent constructor, subclass constructor, this keyword, super keyword, which is worth analyzing and recording.
[java] view
Plaincopy
Package com.lsky.test
Public class Father {
Private int sum
/ / static blocks in the parent class
Static {
System.out.println (Happy New year 2010)
}
Public void first (int I) {
Sum=++i+sum++-1+i--
System.out.println (sum)
}
/ / parent class no-parameter constructor
Public Father () {
System.out.println ("A good Day")
}
/ / Constructor with parameters
Public Father (String s) {
System.out.println ("We are students")
}
/ / Free blocks in the parent class
{
System.out.println ("Bodhi does not have a tree, a mirror is not a Taiwan, there is nothing, where does it cause dust")
}
}
Package com.lsky.test
Public class Son extends Father {
Private int sum
Private static Father father
/ / Constructor with parameters in subclass (constructor with parameters)
Public Son (Father father) {
This ()
Father.first (8)
}
/ / No parameter constructor in subclass (no parameter constructor)
Public Son () {
This.punk (6)
}
Public void first (int I) {
Sum=sum+i++
System.out.println (sum)
}
/ / static blocks in subclasses
Static {
Father f=new Son ()
F.first (4)
}
Int punk (int sum) {
Sum=sum
Return sum
}
/ / Free blocks in subclasses
{
System.out.println ("once experienced difficulties in the sea, except Wushan is not a cloud")
}
Public static void main (String argsp []) {
Son son=new Son (new Son ())
}
}
[java] view
Plaincopy
/ / the output result is:
Happy New year 2010
Bodhi has no tree, the mirror is not Taiwan, there is nothing, where does it cause dust?
Nice day.
No water 's wide enough when you have crossed the sea; No cloud is beautiful but that which crowns the peak
four
Bodhi has no tree, the mirror is not Taiwan, there is nothing, where does it cause dust?
Nice day.
No water 's wide enough when you have crossed the sea; No cloud is beautiful but that which crowns the peak
Bodhi has no tree, the mirror is not Taiwan, there is nothing, where does it cause dust?
Nice day.
No water 's wide enough when you have crossed the sea; No cloud is beautiful but that which crowns the peak
eight
That's all of the article "what is the order in which Java static blocks, free blocks and constructors are executed?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.