When initializing a class, what is the first part of the class and the overall execution order? Similarly, what is the order of the class when it is released? Let's take a look at the following code.
Package net. moon. insignificant. commonclass; </P> <p> class commonsubclass extends commonsupperclass {<br/> static {<br/> system. out. println ("common sub static initial"); <br/>}</P> <p> Public commonsubclass () {<br/> system. out. println ("common sub construct"); <br/>}</P> <p> @ override <br/> protected void finalize () throws throwable {<br/> // todo auto-generated method stub <br/> system. out. println ("common sub finalize"); <br/> super. finalize (); </P> <p >}< br/>}</P> <p> abstract class commonsupperclass {<br/> Public commonsupperclass () {<br/> system. out. println ("common super construct"); <br/>}</P> <p> static {<br/> system. out. println ("common supper static initial"); <br/>}</P> <p> @ override <br/> protected void finalize () throws throwable {<br/> // todo auto-generated method stub <br/> system. out. println ("common supper finalize"); <br/> super. finalize (); </P> <p >}< br/>}</P> <p> public class demo {<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> commonsubclass CSS = new commonsubclass (); <br/> CSS = NULL; <br/> system. GC (); </P> <p >}< br/>
As long as you run the above Code, the result is as follows:
Common supper static initial <br/> common sub static initial <br/> common super construct <br/> common sub finalize <br/> common supper finalize
In fact, we know that the execution order during initialization is:
1. Static code block of the parent class
2. Static code block of subclass
3. constructor of the parent class
4. Subclass Construction Method
When releasing data, the execution sequence is:
1. Finalize method of subclass
2. Finalize method of the parent class
The exception is that when the parent class is an abstract class, the constructor of the parent class is also called. It seems that the abstract class is still instantiated inside the virtual machine.
Next article: Java tip [004]: Can the final class have the protected attribute or method?