This article is to see "effective Java" 17th feeling, on the basis of its expansion. This article explores mainly the following: After class inheritance, create subclass objects, call to parent constructors, and order of class initialization (that is, initialization of the parent class domain, initialization of the subclass domain). For such an analysis, do not have any theoretical theory, my solution is to first make a process to see what the actual situation is, if not with their understanding of the same, and then check the data to supplement their own theoretical system. So no nonsense, look at the code.
Class superclass{public
static String superstaticfield= "parent class = = static Domain";
Public String superfield= "parent = = = Variable";
static{
System.out.println (Superstaticfield);
System.out.println ("parent class = = static Initialization block");
{
System.out.println (superfield);
System.out.println ("parent class = = initialization Block");
Public superclass () {
System.out.println ("Parent class Builder");
}
public class subclass extends superclass {public
static String substaticfield= "Subclass = = static Domain";
Public String subfield= "Subclass = = variable";
static{
System.out.println (Substaticfield);
System.out.println ("Subclass = = Static initialization block");
}
{
System.out.println (subfield);
System.out.println ("Subclass = = Initialize block");
Public subclass () {
System.out.println ("Subclass Builder");
}
public static void Main (string[] args) {
System.out.println ("----------------");
New subclass ();
}
The result of the output is:
Parent class = = static domain
Parent class = = Static Initialization block
Sub class = = static Field
Subclass = = Static initialization block
----------------
Parent class = = Variable
Parent class = = Initialization block
Parent Class Builder
Sub class = = Variable
Subclass = = Initialize Block
Child Class Builder
From the code to the output we can see clearly, we have implemented the inheritance of the class, that first have a parent class, so the Java do also right, is to initialize the parent class first, the parent class of the. class file loaded in, with the parent class and then load the child class. Because the static domain is the domain of the class, it already exists before the object has been compiled, so the static domain of the static domain and subclass of the parent class has already been initialized after the compilation is finished, so the static domain is printed first. The initialization of the generic variable of the class is then, of course, starting with the parent class, and it is also very understandable that the subclass will not be able to initialize the state until all the cases in the parent class have been stabilized, otherwise the subclass cannot use the parent object.
Then we'll do an experiment and annotate all the statements in the main method, and you'll see that the output is only the split line (--------------). This also confirms that the static domain of the class is already initialized at compile time, and is symbiotic with the class, even if the object is not created. That's a pretty good thing, and that's another thing in "effective Java," A static factory, where it's not going to start.
Here we derive other questions from this example, which are also problems with inheritance, and to highlight the problem, let me simplify the code.
import java.util.*;;
Class super{public
Super () {
overrideme ();
}
public void Overrideme () {
}
} public
class Sub extends super{
private final date date;
Sub () {
date=new date ();
}
public void Overrideme () {
System.out.println (date);
}
public static void Main (string[] args) {
System.out.println ("------output:");
Sub Sub=new Sub ();
Sub.overrideme ();
}
What is this output?
-------Output:
Null
Fri Mar 15:59:56 CST 2015
The first output is NULL because the parent class constructor calls the Overrideme () method when the parent class constructor is loaded, and this method is overwritten in the subclass, so the date that has not been initialized is mobilized at the time of the call, and is, of course, null. This problem is not difficult to analyze, the main discussion is that we inherit Java tool class, have not considered this situation, and how to avoid this situation. The practice of effective Java is to extend our classes as much as possible using a combination of forms.