The initialization problem of the Java diary (3) class (a problem with 9CBS)

xiaoxiao2021-03-06  55

I added an output "7" on the original issue; it is convenient to explain the time of the parent class;

Public class test2 extends test1 {{system.out.print ("1");}

TEST2 () {system.out.print ("2");

Static {system.out.print ("3");

{System.out.print ("4");

Public static void main (string [] args) {new test2 ();}}

Class test1 {{stem.out.print ("7");

TEST1 () {system.out.print ("5");

Static {system.out.print ("6");}}

Program execution result: 6375142 Call order: 1, first load the class: initialize the static variable, first initialize the static variable of the parent class, then the static variable of the subclass; First, the initialization of the parent class, as well as the call of the parent class constructor; 3, the last initialized subclass, call subclass construction method;

In Chinese Tij, the construction method of once said that the structure of the child is executed before the subclass constructor, saying that although not wrong, but is not

Improve, it should be that the parent class has been executed before the subclass initialization.

转载请注明原文地址:https://www.9cbs.com/read-93967.html

New Post(0)