`

Something about JVM class loading and initialization

    博客分类:
  • Java
 
阅读更多

Class loading stages:

  • Loading
  • Linking
    • Verification
    • Preparation
    • Resolution
  • Initialization

 

Class Loader:

 

 

 

Class loading mechanism:

  • parent delegation model

     The delegation model requires that any request for a class loader to load a given class is first delegated to its parent class loader before the requested class loader tries to load the class itself. The parent class loader, in turn, goes through the same process of asking its parent. This chain of delegation continues through to the bootstrap class loader (also known as the primordial or system class loader). If a class loader's parent can load a given class, it returns that class. Otherwise, the class loader attempts to load the class itself.

 

When a Class is loaded:

 

"The Lifetime of a Class," different implementations of the Java virtual machine are permitted to perform resolution at different times during the execution of a program. An implementation may choose to link everything up front by following all symbolic references from the initial class, then all symbolic references from subsequent classes, until every symbolic reference has been resolved. In this case, the application would be completely linked before its main() method was ever invoked. This approach is called early resolution. Alternatively, an implementation may choose to wait until the very last minute to resolve each symbolic reference. In this case, the Java virtual machine would resolve a symbolic reference only when it is first used by the running program. This approach is called late resolution. Implementations may also use a resolution strategy in-between these two extremes.

Although a Java virtual machine implementation has some freedom in choosing when to resolve symbolic references, every Java virtual machine must give the outward impression that it uses late resolution. No matter when a particular Java virtual machine performs its resolution, it will always throw any error that results from attempting to resolve a symbolic reference at the point in the execution of the program where the symbolic reference was actually used for the first time. In this way, it will always appear to the user as if the resolution were late. If a Java virtual machine does early resolution, and during early resolution discovers that a class file is missing, it won't report the class file missing by throwing the appropriate error until later in the program when something in that class file is actually used. If the class is never used by the program, the error will never be thrown.

 

When a Class is initialized:

After class loading, initialization of class takes place which means initializing all static members of class. A Class is initialized in Java when :

  • An Instance of class is created using either new() keyword or using reflection using Class.forName(), which may throw ClassNotFoundException in Java.
  • An static method of Class is invoked.
  • An static field of Class is assigned.
  • An static field of class is used which is not a constant variable.
  • If Class is a top level class and an assert statement lexically nested within class is executed.

Reflection can also cause initialization of class. Some methods of java.lang.reflect package may cause class to be initialized. JLS Strictly says that a class should not be initialized by any reason other than above

 

Rules of class initialization:

Now we know what triggers initialization of a class in Java, which is precisely documented in Java language specification. Its also important to know in which order various fields (static and non static), block (static an non static), various classes (sub class and super class) and various Interfaces (sub interface, implementation class and super interface) is initialized in Java. Here are some of the rules of class initialization in Java:

  • Classes are initialized from top to bottom so field declared on top initialized before field declared in bottom
  • Super Class is initialized before Sub Class or derived class in Java
  • If Class initialization is triggered due to access of static field, only Class which has declared static field is initialized and it doesn't trigger initialization of super class or sub class even if static field is referenced by Type of Sub Class, Sub Interface or by implementation class of interface.
  • Interface initialization in Java doesn't cause super interfaces to be initialized.
  • Static fields are initialized during static initialization of class while non static fields are initialized when instance of class is created. It means static fields are initialized before non static fields in Java.
  • Non-static fields are initialized by constructors in Java. Sub class constructor implicitly call super class constructor before doing any initialization, which guarantees that non static or instance variables of super class is initialized before sub class
分享到:
评论

相关推荐

    jvm 加载class文件

    ### JVM加载Class文件详解 #### 一、Java与JVM中的Class文件加载机制概述 Java作为一种动态性极强的解释型编程语言,在程序运行时,Java虚拟机(JVM)负责将编译生成的`.class`文件加载到内存中进行执行。在Java...

    Introduction to JVM Languages

    Understand the popular JVM languages and the Java Class Library Find out about various programming paradigms such as imperative, object oriented, and functional Work with common JVM tools such as ...

    JVM加载class文件的原理机制

    JVM加载class文件的原理机制 JVM加载class文件的原理机制是Java中的核心机制之一,由于Java中的所有类必须被装载到JVM中才能运行,这个装载工作是由JVM中的类装载器完成的。类装载器所做的工作实质是把类文件从硬盘...

    jvm解析编译过的class文件

    Java虚拟机(JVM)是Java程序运行的基础,它负责解析和执行编译后的.class文件。这个过程涉及多个阶段,包括加载、验证、准备、解析和初始化。在本篇文章中,我们将深入探讨JVM如何解析并执行这些编译过的类文件。 ...

    动态编译字符串成java,并且添加class到jvm

    在Java编程中,动态编译字符串成Java代码并将其加载到JVM(Java虚拟机)是一种高级技巧,常用于运行时代码生成、元编程或插件系统等场景。这一技术的核心在于利用Java的反射API和Java Compiler API。下面将详细阐述...

    jcl.zip_Creating_class loading java_jcl

    It is the class responsible for finding and loading class files at run time. Creating your own ClassLoader lets you customize the JVM in useful and interesting ways, allowing you to completely ...

    JVM-CLASS文件分析脑图

    JVM-CLASS文件分析脑图的知识点涵盖了Java类文件的结构、组成、以及如何解析这些结构。class文件是Java虚拟机(JVM)能够识别和加载的二进制格式,它包含了一个Java类或者接口的定义。 首先,class文件以一个魔术数...

    jvm memory management and garbage collector

    很久之前就一直在学习JVM,但是一直也没有好好的总结,最近终于有了空闲,将之前学习的内容整理成了一个PPT。PPT也可以在这里下载: https://github.com/hitynsun/docs/tree/master/JVM 也希望大神们可以批评指正...

    JVM加载class文件的原理机制.pdf

    JVM加载class文件的原理机制 JVM加载class文件的原理机制是Java虚拟机中一个非常重要的组件,负责将class文件加载到内存中,以便Java程序的执行。下面是JVM加载class文件的原理机制的详细介绍: 类加载的原理 在...

    Java Classloading Mechanism : ClassLoader & ASM & 动态字节码增强

    1. 加载(Loading):查找并读取类的二进制数据,通常是来自.class文件。 2. 验证(Verification):确保字节码符合Java语法规则且不会危害JVM的安全。 3. 准备(Preparation):为类的静态变量分配内存并初始化它们...

    推荐一些JVM原理,JVM调优,JVM内存模型,JAVA并发 电子书1

    标题中提到了JVM原理、JVM调优、JVM内存模型和JAVA并发,这些都是Java虚拟机(JVM)相关的核心概念。JVM是运行Java字节码的虚拟计算机,为Java提供了一个跨平台的环境,确保Java程序可以在不同的操作系统上运行而...

    jdk,jvm源码

    Java虚拟机(JVM)是Java程序运行的核心,它负责解释和执行字节码,为Java应用程序提供了一个跨平台的运行环境。JDK(Java Development Kit)包含了开发和运行Java程序所需的所有工具,包括JVM。当我们谈论"jdk,jvm...

    jvm 启动过程 JVM 原理

    Java虚拟机(JVM)是Java程序运行的基础,它是一个抽象的计算机系统,负责执行Java字节码。本文将深入探讨JVM的启动过程及其基本原理。 首先,我们需要理解JVM的基本概念。JVM是Java Virtual Machine的缩写,它是...

    JVM基础.doc

    **ClassLoader** 负责加载ClassFile到JVM中,它遵循双亲委派模型。常见的ClassLoader包括Bootstrap ClassLoader、Extension ClassLoader和App ClassLoader。 #### 四、内存模型、锁与同步 **Java内存模型** 主要...

    JVM课件(云析学院JVM课程课件)

    综上所述,文件中提供的内容是对JVM基础知识的一个概览,涵盖了Class文件格式、字节码、类加载机制、运行时数据区、垃圾回收策略、以及JVM在其他语言中的应用等多个方面。高级篇和优化篇预计会对JVM的高级特性和调优...

    JVM之用Java解析class文件共10页.pdf.zi

    本文将深入探讨JVM如何通过Java解析class文件,揭示其内部工作原理,以及如何实现这一过程。我们主要关注的是标题所提及的"JVM之用Java解析class文件"这一主题,这涉及到Java程序编译后的二进制表示以及JVM的类加载...

    jvm 详细介绍,了解jvm各个组成部分和功能

    ### JVM 详细介绍:掌握 JVM 的各个组成部分与功能 #### 一、Java 源文件编译及执行 Java 应用程序的核心在于源文件的编译与执行。不同于 C/C++ 这类需要针对不同平台进行编译的语言,Java 采用了一种更为灵活的...

    JVM中编译Class、内存回收、多线程原理和使用

    1. 类加载(Class Loading): 类加载是JVM将Java源代码编译成的`.class`文件加载到内存中的过程。这个过程通常由类加载器(ClassLoader)负责。类加载器可以是系统类加载器,也可以是用户自定义的加载器。加载过程...

    java_FAQ.rar_Something About You

    "java_FAQ.rar_Something About You" 提供的"java FAQ.pdf"很可能包含了一系列关于Java编程的常见问题与解答,帮助开发者解决他们在学习和实践中遇到的问题。下面我们将深入探讨一些Java的核心知识点。 1. **Java...

Global site tag (gtag.js) - Google Analytics