1. If the base class has a default constructor, the default constructor of the base class will be called before the subclass construction:
Class A {<br/> A () {<br/> system. out. println ("A create"); <br/>}</P> <p> Class B extends a {<br/> B () {<br/> // The default constructor of A is called here <br/> system. out. println ("B create"); <br/>}</P> <p> Class C extends a {<br/> C (int I) {<br/> // The default constructor of A is called here <br/> system. out. println ("C create "); <br/>}</P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> B = new B (); <br/> C = new C (10 ); <br/>}</P> <p> // The output is: <br/> A create <br/> B create <br/> A create <br/> C create <br/>
2. If the base class only has constructors with parameters, the subclass must explicitly call the base class constructor through Super (...) in its own constructor:
Class A {<br/> A (int I) {<br/> system. out. println ("A create"); <br/>}</P> <p> Class B extends a {<br/> B () {<br/> // The non-default constructor of the parent class must be explicitly called here; otherwise, compilation fails. <br/> // note that this call can only be placed at the beginning, otherwise, compilation fails. <br/> super (20); <br/> system. out. println ("B create"); <br/>}</P> <p> Class C extends a {<br/> C (int I) {<br/> // The non-default constructor of the parent class must be explicitly called here; otherwise, compilation fails. <br/> // note that this call can only be placed at the beginning, otherwise, compilation fails. <br/> super (30); <br/> system. out. println ("C create "); <br/>}</P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> B = new B (); <br/> C = new C (10 ); <br/>}< br/> // The output is: <br/> A create <br/> B create <br/> A create <br/> C create <br/>
3. The above only describes the simplest sequence of calling constructor. In fact, the actual initialization process of an object should be:
- Initializes the object's bucket to a binary value of 0.
- First, recursive to the base class at the top, and use the base class at the top as the current class.
- For the current class:
- Call the initial setup code of the member variable in declared order.
- Call the constructor.
- Next, take the next inherited class as the current class. Continue step 3.
Let's take a look at the following code:
Class A {<br/> A () {<br/> system. out. println ("A create"); <br/>}</P> <p> Class D {<br/> D () {<br/> system. out. println ("d create "); <br/>}</P> <p> Class B extends a {<br/> private d = new D (); <br/> B () {<br/> system. out. println ("B create"); <br/>}</P> <p> Class C extends B {<br/> C (int I) {<br/> system. out. println ("C create "); <br/>}</P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> C = new C (10); <br/>}< br/>
The initialization process is like this:
- First recursion from C to B, then recursion from B to.
- A has no member variables, so the constructor of A is called.
- After B is returned, B has a member of class D with initialization, so the constructor of class D is called.
- Then the constructor of B is called.
- Finally, return to C, and the constructor of C is called.
Therefore, the output should be:
A create
D create
B create
C create
4. be careful when calling virtual functions in Constructor (common functions in Java are virtual), especially in constructors of base classes, at this time, the inherited class members may not have been initialized yet:
Class A {<br/> A () {<br/> system. out. println ("A create"); <br/> proc (); <br/>}< br/> Public void proc () {<br/>}</P> <p> Class B extends a {<br/> private int I; <br/> B () {<br/> system. out. println ("B create"); <br/> I = 10; <br/>}< br/> Public void proc () {<br/> system. out. println (I); <br/>}</P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> B = new B (); <br/>}< br/> output: <br/> A create <br/> 0 <br/> B create <br/>The constructor of A calls Proc. At this time, the constructor of B has not been called, so I has not been assigned to 10, and the final output result is 0.
5. java objects are cleared through the garbage collection mechanism, so Java classes do not have destructor. When you need to clear Resources in the class, you can declare a function, such as dispose, call it when appropriate.