The process of creating and initializing classes in Java has two kinds of situations, one is the creation and initialization of single class, the other is the creation and initialization of parent-child class with inheritance relation.
First of all, simple, single-class creation and initialization process. In Java we all know that most objects are created by using the New keyword, when we are in our own
In the code, write
New ClassName ();//Create an instance of the ClassName class
, the interpreter will tailor a memory space for classname when it intercepts the new keyword, and this is when the memory space is allocated for all member variables in the class.
And the most primitive initialization, all reference types are made into null base data type 0, after which the interpreter will continue to interpret execution to ClassName ();
constructor that invokes the constructor of the specified class (initializes the object based on the user's needs). However, there is a member variable that is not fully initialized during this process, and this member variable is
A static member variable that is initialized when the class static property or method is first called or when the class is first created.
Next is the creation and initialization of the parent-child class under the inheritance relationship. There is an old saying in China: "No father, no son." The class created under the inheritance relationship in Java has a formal proof of this sentence.
When we try to create a subclass, the Java interpreter discovers that the class inherits other classes, so it will first create its parent class, and remember that it does not allocate any memory space for the subclass at this time.
Instead of creating the parent class directly over your own creation, the Java interpreter will continue to create the parent class if you check that the parent class also inherits other classes. Until the last root parent class
The child class is not created until the memory is allocated. The invocation of the constructor method starts with the subclass, but the constructor of the parent class must be called in the constructor of the subclass, and we often don't see
The constructor method for calling the parent class is shown in the constructor method of the subclass, because the interpreter implicitly calls the default parameterless constructor of the parent class, but when we override the default of the parent class by overloading the mechanism
When the construction method overrides, then the constructor of the calling parent class must be displayed in the child class
The creation and initialization of classes in Java