Java-The full process of initialization

Java类加载与初始化
本文详细介绍了Java中类的加载过程及其初始化顺序,包括静态成员的初始化时机与构造函数的调用顺序,以及继承关系中基类与派生类的初始化流程。

由于Java中的所有事物都是对象, 所以许多动作就变得更加容易, 加载动作仅仅是其中之一.
Java中每个类的编译代码都存在于它自己的独立的文件中. 该文件只在需要使用程序代码时才会被加载. 可以说:"类的代码在初次使用时才加载". 这通常是指直到类的第一个对象被构建时才发生加载, 但是当访问static字段方法时, 其也会发生加载.
初次使用之处也是static初始化发生之处. 所有的static对象和static代码段都会在加载时依程序中的顺序(即定义类时的书写顺序)而依次初始化. 当然, 定义为static的东西只会被初始化一次.

/**
 * Title: The full process of initialization<br>
 * Description: 了解包括Inheritance在内的初始化全过程, 以对所发生的一切有个全局性的把握<br>
 * Copyright: (c) 2008 Thinking in Java<br>
 * Company: Augmentum Inc<br>
 * 
@author Forest He
 * 
@version 1.0
 
*/

class Insect 
    
private int i = 9
    
protected int j; 
    Insect() 

        System.out.println(
"i=" + i + ",j=" + j); 
        j
=37
    }
 
    
private static int x1 = print("static Insect.x1.initialized"); 
    
public static int print(String s) 
        System.out.println(s);
        
return 47;
    }

}
 

class Bettle extends Insect 
    
private int k = print("Bettle.k initialized"); 
    
public Bettle() {
        System.out.println(
"k=" + k); 
        System.out.println(
"j=" + j); 
    }
 
    
private static int x2=print("static Bettle.x2 initialized"); 
    
public static void main(String [] args) 
        System.out.println(
"Bettle constructor"); 
        Bettle b 
= new Bettle();
    }

}

上例中, 在Beetle上运行Java时, 所发生的第一件事就是试图访问Beetle.main()(一个static方法), 于是加载器开始启动并找出Beetle类的编译代码(它被编译到了一个名为Beetle.class的文件中). 在对Beetle进行加载的过程中, 编译器注意到它有一个基类(这是由extends关键字得知的), 于是Beetle继续进行加载(对基类加载). 不管你是否打算产生一个该基类的对象, 这都要发生.
如果该基类还有其自身的基类, 那么第二个基类就会被加载, 如此类推. 接下来, 根基类中的static初始化(在此例中为Insect)即会被执行, 然后是下一个导出类, 依次类推. 这种方式很重要, 因为导出类的static初始化可能会依赖于基类成员能否被正确初始化.

至此为止, 必要的类都已加载完毕, 对象就可以被创建了. 首先, 基类对象中的所有基本类型和对象都会被设为缺省值; 然后, 基类Constructor会被调用(自动调用or人工通过super()调用). 在基类Constructor完成之后, 导出类成员按其次序被初始化, 最后导出类Constructor执行.

FAILURE: Build failed with an exception. * What went wrong: Unable to start the daemon process. This problem might be caused by incorrect configuration of the daemon. For example, an unrecognized jvm option is used.For more details on the daemon, please refer to https://docs.gradle.org/8.10.2-milestone-1/userguide/gradle_daemon.html in the Gradle documentation. Process command line: /usr/lib/jvm/java-11-openjdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country=CA -Duser.language=en -Duser.variant -cp /home/ts/.gradle/wrapper/dists/gradle-8.10.2-milestone-1-bin/9qvixjbuxjevivtpr9fhsty1d/gradle-8.10.2-milestone-1/lib/gradle-daemon-main-8.10.2.jar -javaagent:/home/ts/.gradle/wrapper/dists/gradle-8.10.2-milestone-1-bin/9qvixjbuxjevivtpr9fhsty1d/gradle-8.10.2-milestone-1/lib/agents/gradle-instrumentation-agent-8.10.2.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.10.2-milestone-1 Please read the following process output to find out more: ----------------------- FAILURE: Build failed with an exception. * What went wrong: org/objectweb/asm/Type * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. * Exception is: java.lang.NoClassDefFoundError: org/objectweb/asm/Type at org.gradle.initialization.DefaultLegacyTypesSupport.<clinit>(DefaultLegacyTypesSupport.java:37) at org.gradle.internal.service.scopes.WorkerSharedGlobalScopeServices.createLegacyTypesSupport(WorkerSharedGlo
03-27
2025-07-16 16:10:00.282 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframe work.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eduCourseServiceImpl': Unsatisfied dependency expressed through field 'eduVideoService': No qualifying bean of type ' com.csxy.nsh.eduservice.service.EduVideoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2025-07-16 16:10:00.282 [main] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-0} closing ... 2025-07-16 16:10:00.284 [main] INFO org.apache.catalina.core.StandardService - Stopping service [Tomcat] 2025-07-16 16:10:00.329 [main] INFO o.s.b.a.logging.ConditionEvaluationReportLogger - Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-07-16 16:10:00.343 [main] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: Field eduVideoService in com.csxy.nsh.eduservice.service.impl.EduCourseServiceImpl required a bean of type 'com.csxy.nsh.eduservice.service.EduVideoService' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.csxy.nsh.eduservice.service.EduVideoService' in your configuration. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.514 s [INFO] Finished at: 2025-07-16T16:10:00+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.0.13:run (default-cli) on project service-edu: Process terminated with exit code: 1 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
07-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值