简体   繁体   English

按需初始化持有人习惯用法-何时加载类?

[英]Initialization-on-demand holder idiom - When are classes loaded?

I have been looking at: https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom to understand a little bit more about Singletons. 我一直在看: https : //en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom来了解有关Singletons的更多信息。

My question is when exactly does the static inner class get Loaded and when does it get Initialised? 我的问题是静态内部类何时确切地被加载,什么时候被初始化? My understanding is that classes can be loaded but remain uninitialised until initialisation is absolutely necessary. 我的理解是,可以加载类,但始终保持未初始化状态,直到绝对需要初始化为止。

If the class is not loaded, how is the private static inner class specified within the JVM? 如果未加载该类,那么如何在JVM中指定私有静态内部类?

The exact time when a class is initialized, is specified in the Java® Language Specification, §12.4.1 Java®语言规范§12.4.1中指定了初始化类的确切时间。

§12.4.1. §12.4.1。 When Initialization Occurs 发生初始化时

A class or interface type T will be initialized immediately before the first occurrence of any one of the following: 类或接口类型T将在以下任何一种首次出现之前立即初始化:

  • T is a class and an instance of T is created. T是一个类,并创建T的实例。
  • A static method declared by T is invoked. 调用由T声明的static方法。
  • A static field declared by T is assigned. 分配由T声明的static字段。
  • A static field declared by T is used and the field is not a constant variable ( §4.12.4 ). 使用由T声明的static字段,并且该字段不是常量变量(第4.12.4节 )。
  • T is a top level class ( §7.6 ) and an assert statement ( §14.10 ) lexically nested within T ( §8.1.3 ) is executed. T是一个顶级类(第7.6节 ),并执行一个词法嵌套在Tassert语句(第14.10节 )(第8.1.3节 )。

When a class is initialized, its superclasses are initialized (if they have not been previously initialized), as well as any superinterfaces ( §8.1.5 ) that declare any default methods ( §9.4.3 ) (if they have not been previously initialized). 初始化类时,将初始化其超类(如果之前尚未对其进行初始化),以及声明了任何默认方法的任何超接口(第8.1.5节 )(第9.4.3节 )(如果以前未对其进行初始化)。 )。 Initialization of an interface does not, of itself, cause initialization of any of its superinterfaces. 接口的初始化本身并不会导致其任何超级接口的初始化。

The last bullet has been removed in Java 9 Java 9中删除了最后一个项目符号

The time of class loading is not that fixed and may depend on implementation details, eg how the verifier has been implemented. 加载的时间不是固定的,并且可能取决于实现细节,例如,验证程序的实现方式。 But obviously, it has to happen before the initialization. 但显然,它必须在初始化之前发生。

From the JVM's point of view, the fact that this is a nested class has no special relevance. 从JVM的角度来看,这是一个嵌套类这一事实没有特殊的意义。 There is a symbolic reference to the inner class in the outer class' constant pool , like there is for any other referenced class. 在外部类的常量池中有对内部类的符号引用,就像其他任何引用的类一样。 It will be resolved when needed. 它将在需要时解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正确实施按需初始化持有人习惯用法 - Correct implementation of initialization-on-demand holder idiom Singleton使用“按需初始化持有人惯用语” - Singleton using “Initialization-on-demand holder idiom” 初始化按需持有人习语的疑问 - Doubts in Initialization-on-demand holder idiom 如果参数化,按需初始化持有人习惯仍然安全吗? - Is initialization-on-demand holder idiom still safe if parameterized? 点播初始化持有人成语能否导致部分创建的对象? - Can Initialization-on-demand holder Idiom result in a partially created object? Initialization On Demand Holder 习惯用法 - Initialization On Demand Holder idiom 如何对按需初始化持有者实例的字段进行垃圾收集/清除? - How to garbage collect/clear fields of initialization-on-demand holder instance? 延迟加载 singleton:双重检查锁定与按需初始化持有人习惯用法 - Lazy-loaded singleton: Double-checked locking vs Initialization on demand holder idiom 初始化按需持有者惯用线程安全,没有最终修饰符 - Is Initialization On Demand Holder idiom thread safe without a final modifier 按需初始化多个没有静态嵌套类的静态变量? - Initialization on demand for multiple static variables without static nested classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM