Java test required question (1) class initialization sequence

Source: Internet
Author: User

When you go to an interview, you will often encounter such questions: give you two classes of code, which are inherited between them.
Link, each class only contains the constructor method and some variables. The constructor may have a piece of code to perform a certain
And some code that outputs the variable value to the console. Then let's determine the output result. The actual
This section describes the class initialization sequence in the case of inheritance.
We all know that for static variables, static initialization blocks, variables, initialization blocks, constructors, their initialization
The sequence is (static variables, static initialization blocks)> (variables, initialization blocks)> constructor. You can also
To verify this:
Java code
Public class initialordertest {
// Static variables
Public static string staticfield = "static variable ";
// Variable
Public String Field = "variable ";
// Static initialization Block
Static {
System. Out. println (staticfield );
System. Out. println ("static initialization block ");
}
// Initialize the block
{
System. Out. println (field );
System. Out. println ("initialization block ");
}
// Constructor
Public initialordertest (){
System. Out. println ("constructor ");
}
Public static void main (string [] ARGs ){
New initialordertest ();
}
}
Run the above code and we will get the following output:
1. Static variables
2. Static initialization Block
3. Variables
4. initialize the block
5. Constructor
This is exactly the same as what we mentioned above. What will happen with inheritance? We still get it with a piece of test code
Obtain the final result:
Java code:
Class Parent {
// Static variables
Public static string p_staticfield = "parent class -- static variable ";
// Variable
Public String p_field = "parent class -- variable ";
// Static initialization Block
Static {
System. Out. println (p_staticfield );
System. Out. println ("parent class -- Static initialization block ");
}
// Initialize the block
{
System. Out. println (p_field );
System. Out. println ("parent class -- initialization block ");
}
// Constructor
Public parent (){
System. Out. println ("parent class -- constructor ");
}
}
Public class subclass extends parent {
// Static variables
Public static string s_staticfield = "subclass -- static variable ";
// Variable
Public String s_field = "subclass -- variable ";
// Static initialization Block
Static {
System. Out. println (s_staticfield );
System. Out. println ("subclass -- Static initialization block ");
}
// Initialize the block
{
System. Out. println (s_field );
System. Out. println ("subclass -- initialization block ");
}
// Constructor
Public subclass (){
System. Out. println ("subclass -- constructor ");
}
// Program entry
Public static void main (string [] ARGs ){
New subclass ();
}
}
Run the above Code and the result is immediately displayed in front of our eyes:
1. parent class-static variables
2. parent class-static initialization Block
3. Subclass-static variable
4. Subclass-static initialization Block
5. parent class -- Variable
6. parent class -- initialize the block
7. parent class -- Constructor
8. Subclass -- Variable
9. Subclass -- initialize the block
10. Subclass -- Constructor
Now the results are self-explanatory. You may notice that the parent class is not fully initialized.
In fact, the static variables of the subclass and the initialization block of the static initialization block are the variables of the parent class,
The initialization block and constructor are completed before initialization.
So what is the sequence between static variables and static initialization blocks, and between variables and initialization blocks? Whether
Static variables always start with static initialization blocks, and variables are always initialized before initialization blocks? Actually it depends on
The order in which they appear in the class. Take static variables and static initialization blocks as examples.
Similarly, we should write a class for testing:
Java code
Public class testorder {
// Static variables
Public static Testa A = new TESTA ();
// Static initialization Block
Static {
System. Out. println ("static initialization block ");
}
// Static variables
Public static testb B = new testb ();
Public static void main (string [] ARGs ){
New testorder ();
}
}
Class Testa {
Public TESTA (){
System. Out. println ("test -- ");
}
}
Class testb {
Public testb (){
System.

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.