Java initialization and dynamic binding

Source: Internet
Author: User

Initialization sequence:

1. static member initialization;

Initialize the static members in the parent class (including static field values and static statement blocks, in the declared order), and then initialize the Child class members. This will be traced back to the top-level base class.

Static member initialization occurs when the class is loaded, and the constructor may not be called. This class is loaded when a program tries to access a class.

2. Non-static member initialization;

3. Call the constructor.

Note that the Order 2 and 3 is the initialization of non-static members of the parent class ----> parent class constructor -----> initialization of non-static members of the subclass -----> subclass constructor.
Example:


[Java]
Public class Father {
 
Private static int fa_static1 = printInit ("static value in father! ");
Private int fa_instance1 = printInit ("instance value in father! ");
Static
{
PrintInit ("static block in father! ");
}
Private static int fa_static2 = printInit ("another static value in father! ");

{
PrintInit ("common block in father! ");
}

Private int fa_instance2 = printInit ("another instance value in father! ");

Public Father (){
System. out. println ("default father constructor! ");
}
Public Father (int I ){
System. out. println ("father constructor with parameter I =" + I );
}

Protected static int printInit (String s ){
System. out. println (s );
Return 1;
}
 
}

Public class Father {

Private static int fa_static1 = printInit ("static value in father! ");
Private int fa_instance1 = printInit ("instance value in father! ");
Static
{
PrintInit ("static block in father! ");
}
Private static int fa_static2 = printInit ("another static value in father! ");
 
{
PrintInit ("common block in father! ");
}
 
Private int fa_instance2 = printInit ("another instance value in father! ");
 
Public Father (){
System. out. println ("default father constructor! ");
}
Public Father (int I ){
System. out. println ("father constructor with parameter I =" + I );
}
 
Protected static int printInit (String s ){
System. out. println (s );
Return 1;
}

}
[Java]

Public class Son extends Father {
 
Private static int son_static1 = printInit ("static value in son! ");
Private int son_instance1 = printInit ("instance value in son! ");
Static
{
PrintInit ("static block in son! ");
}
Private static int son_static2 = printInit ("another static value in son! ");

{
PrintInit ("common block in son! ");
}
Private int son_instance2 = printInit ("another instance value in son! ");

Public Son (){
System. out. println ("default son constructor! ");
}

Public Son (int I ){
Super (I );
System. out. println ("son constructor with parameter I =" + I );
}

Public static void main (String [] args ){
System. out. println ("------ test1 -----------");
Father. printInit ("java ");
System. out. println ("------ test2 -----------");
Son s1 = new Son ();
System. out. println ("------ test3 -----------");
Son s2 = new Son (2 );
}
}

Public class Son extends Father {

Private static int son_static1 = printInit ("static value in son! ");
Private int son_instance1 = printInit ("instance value in son! ");
Static
{
PrintInit ("static block in son! ");
}
Private static int son_static2 = printInit ("another static value in son! ");
 
{
PrintInit ("common block in son! ");
}
Private int son_instance2 = printInit ("another instance value in son! ");
 
Public Son (){
System. out. println ("default son constructor! ");
}
 
Public Son (int I ){
Super (I );
System. out. println ("son constructor with parameter I =" + I );
}
 
Public static void main (String [] args ){
System. out. println ("------ test1 -----------");
Father. printInit ("java ");
System. out. println ("------ test2 -----------");
Son s1 = new Son ();
System. out. println ("------ test3 -----------");
Son s2 = new Son (2 );
}
} [Plain] view plaincopyprint? Static value in father!
Static block in father!
Another static value in father!
Static value in son!
Static block in son!
Another static value in son!
------ Test1 -----------
Java
------ Test2 -----------
Instance value in father!
Common block in father!
Another instance value in father!
Default father constructor!
Instance value in son!
Common block in son!
Another instance value in son!
Default son constructor!
------ Test3 -----------
Instance value in father!
Common block in father!
Another instance value in father!
Father constructor with parameter I = 2
Instance value in son!
Common block in son!
Another instance value in son!
Son constructor with parameter I = 2

Static value in father!
Static block in father!
Another static value in father!
Static value in son!
Static block in son!
Another static value in son!
------ Test1 -----------
Java
------ Test2 -----------
Instance value in father!
Common block in father!
Another instance value in father!
Default father constructor!
Instance value in son!
Common block in son!
Another instance value in son!
Default son constructor!
------ Test3 -----------
Instance value in father!
Common block in father!
Another instance value in father!
Father constructor with parameter I = 2
Instance value in son!
Common block in son!
Another instance value in son!
Son constructor with parameter I = 2 parsing:

Test1: run the Son. java file and find the function entry main. This is the static function that tries to access Son. The Son class needs to be loaded and the static volume is initialized during loading. The parent class Father is obtained during the loading process. Therefore, you must first initialize the static volume of the parent class. After the static volume initialization of the two classes is complete, run the main method to output test1.

The output result shows that the initialization sequence of static member variables and static statement blocks is the same as the Declaration sequence.

Test2: the constructor without parameters automatically calls the constructor of the parent class. The member variables are initialized before the constructor is called.

 

 

Static binding and dynamic binding:

Concept:

Binding means that a method call is associated with the class (method subject) of the method. For java, binding is divided into static binding and dynamic binding, or pre-binding and post-binding.

If the program is bound before execution, it is implemented by the compiler and the linked program, which is called pre-binding. There is only one method call in C language, that is, pre-binding.

During runtime, objects are bound based on their types, which are called post-binding, also known as dynamic binding or runtime binding.

In Java, except for the static and final methods (private methods are automatically considered as final methods), all other methods are later bound.

Later binding is the basis of polymorphism implemented in java.

Dynamic binding process: the virtual machine extracts the actual method table of the object, the virtual machine search method signature, and the call method.

[Note] binding only applies to methods. It does not work for domain values. The declared classes use domain values. At the same time, only methods that both the parent class and the Child class can be dynamically bound.

[Java]
Public class Father {

Protected String name = "parent attribute ";

Public String getName (){
Return name;
}
}
Public class Son extends Father {
 
Protected String name = "son attributes ";
Protected String sonName = "attributes of only sons ";
Public String getSonName (){
Return sonName;
}
 
Public String getName (){
Return name;
}

 
Public static void main (String [] args ){
Father fa = new Son ();
System. out. println ("member call:" + fa. name );

// Dynamic binding is only applicable to methods. The attribute is still the parent attribute.
System. out. println ("method call:" + fa. getName ());
// System. out. println ("member call:" + fa. sonName); // cannot be called
// Cannot be called. This method is not available in father.
// System. out. println ("method call:" + fa. getSonName ());
}
}

Public class Father {
 
Protected String name = "parent attribute ";
 
Public String getName (){
Return name;
}
}
Public class Son extends Father {

Protected String name = "son attributes ";
Protected String sonName = "attributes of only sons ";
Public String getSonName (){
Return sonName;
}

Public String getName (){
Return name;
}
 

Public static void main (String [] args ){
Father fa = new Son ();
System. out. println ("member call:" + fa. name );
 
// Dynamic binding is only applicable to methods. The attribute is still the parent attribute.
System. out. println ("method call:" + fa. getName ());
// System. out. println ("member call:" + fa. sonName); // cannot be called
// Cannot be called. This method is not available in father.
// System. out. println ("method call:" + fa. getSonName ());
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.