Successful order of members initialization

xiaoxiao2021-03-06  15

ClassLoaderDemo.java:

Package test;

Class superLoader {INT i = seti (); static int j = setj (); int z; superloader () {system.out.println ("superloader @ constructor @ z 2 initialized."); this.z = 5;} Public int seti () {system.out.println ("SuperLoader @ i @ initialized."); Return 10;} public static int setj () {system.out.println ("SuperLoader @ J @ INITIRIZED."); Return 15;}}

public class ClassLoaderDemo extends SuperLoader {int a = setA (); static int b = setB (); int c; ClassLoaderDemo () {System.out.println ( "ClassLoaderDemo @ constructor @ c @ initialized."); this.c = 0;} public int seta () {system.out.println ("ClassLoaderDemo @ a @ initialized."); Return 20;} public static int setb () {system.out.println ("ClassLoaderDemo @ b @ @ @ @ @ @ (} Public static void main (string [] args) {classloaderdemo cld = new classloaderdemo ();}}

result:

SuperLoader @ j @ initialized.ClassLoaderDemo @ b @ initialized.SuperLoader @ i @ initialized.SuperLoader @ constructor @ z 2 initialized.ClassLoaderDemo @ a @ initialized.ClassLoaderDemo @ constructor @ c @ initialized.

analysis:

First, when you start loading the ClassLoaderDemo class, it is found that it has a parent SuperLoader. So I first loaded SuperLoader and found that the class has a static variable j, initialize it, and completes loading.

Second, after completing the load of the SuperLoader class, turn over the head to load ClassLoaderDemo, found that it has static variable B, initialize it, and completes loading.

Third, began to create an instance of the ClassLoaderDemo class to allocate memory space. It is found that it has a parent SuperLoader, first initialize the parent class. First initialize the instance variable I, and then call the constructor of the parent class to initialize the variable z. Complete the parent class initialization. 4. After the parent class is initialized, the ClassLoaderDemo is initialized after the application.

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

New Post(0)