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
相关推荐
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 ...
Java 语言是一种具有动态性的解释型编程语言,当指定程序运行的时候, Java 虚拟机就将编译生成的 .
JVM Memory Model and GC.pdf
JVM加载class文件的原理机制 Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中
动态编译字符串成java,并且添加class到jvm
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,但是一直也没有好好的总结,最近终于有了空闲,将之前学习的内容整理成了一个PPT。PPT也可以在这里下载: https://github.com/hitynsun/docs/tree/master/JVM 也希望大神们可以批评指正...
Dynamic Class Loading in the Java Virtual Machine
学习关于JVM基础,java内模型的全面透析,Jar hell问题以及解决办法,Class文件格式 • Java编译执行流程 • ClassFile的格式介绍 • ClassFile中FieldInfo和MethodInfo介绍 • 类型描述Descriptor介绍 • ClassFile...
JVM负责装载class文件并执行,因此,首先是JDK如何将Java代码编译为class文件、如何装载class文件及如何执行class,将源码编译为class文件的实现取决于各个JVM实现或各种源码编译器。class文件通常由类加载器...
NULL 博文链接:https://jie-bosshr.iteye.com/blog/1364939
Jvm之用C#解析class文件
《 从NoSuchMethodError看jvm编译和class加载方式》的测试项目代码
jvm源码
JVM加载class文件的原理机制.pdf
jvm 配置jvm参数 配置jvm参数
EMCS JVM Integration for Java and other JVM based languages
JVM加载class文件的原理机制JVM加载class文件的原理机制 JVM中类的装载是由类加载器(ClassLoader)和它的子类来实现的,Java中的类加
jvm源码,jvm-native的源码,jvm支行机制,可对jvm的运行过程进行分析 个人网站:https://www.zhangjunbk.com
分析记录了class文件内部的文件结构以及分析过程,上传到CSDN当一个记录用,有问题不要喷呀,一起学习进步