Java static code block and Construction Method execution sequence

Source: Internet
Author: User

Let's take a look at the following classes and then judge their output.
Public class {

Static {
System. out. print (1 );
}
Public (){
System. out. print (2 );
}
}

Public class B extends {

Static {
System. out. print ("");
}
Public B (){
System. out. print ("B ");
}
}

Public class C {

Public static void main (String [] args ){
A a = new B ();
A = new B ();
}
}
Bytes ----------------------------------------------------------------------------------------------
Execution sequence of parent class and Child class
Execution characteristics of static variables
Precautions for override
1.
When both the parent class and subclass have static code blocks and constructors, the execution sequence is as follows:
Parent static code block> subclass static code block
Parent class constructor> subclass Constructor (first father, then child)
For multi-level inheritance relationships, the parent class at the highest level is executed first, and then progressively decreases.

Conclusion: static execution is preferred, and parent class execution is preferred.
Note: The static code block is executed when the JVM loads the class, and the static code block is executed only once.

2.
When calling a method in a class, you must assign values to the member variables in the class before executing the method body. If no specific value is assigned to the Code, the default value is also available. The assignment order of member variables is in the order of forward and backward.
If there are both direct values and constructor values, the values are executed sequentially.

3.
Overload)
Overload is the method name, and the return type is the same. The only difference is that the parameters of the method are different (the parameter type is different, or the parameter type is different)

Rewrite:
1. The return values of the two methods in which method rewriting occurs. The method name and parameter list must be completely consistent.
2. The exception thrown by the subclass cannot exceed the exception thrown by the corresponding method of the parent class.
3. The access level of the subclass method cannot be lower than the access level of the corresponding method of the parent class (public, package, protected, private)

Polymorphism: the reference of the parent class points to the object of the subclass. The method executed during the call is also the method of the subclass. The method of the parent class will not be executed.

The so-called polymorphism means that the reference of the parent class or interface type can point to the instance (object) of the subclass or class that implements the interface)

After reading the above instructions, you should also know the output results of the above program.
Output result: 1a2b2b

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.