This article mainly studies active and passive citations of classes in Java, as detailed below.
Active citation , here are five scenarios of active citation
1. When encountering the four bytecode instructions of new, getstatic, putstatic, and invokestatic, if the class is not initialized, it will be initialized, create an object, read or set static fields, and call static methods.
2. Reflection
3. The parent class will be initialized before the subclass is initialized.
4. A class containing the main method will be initialized when the virtual machine starts.
5. When using dynamic language support of jdk (unknown)
Passive quote :
class SuperClass{ static{ syso("super init"); }public static int value=123;}class SubClass extends SuperClass{ static{ syso("sub init")}}public class ConstantClass{ public static final HW="helloworld";}public class NotInitialization{ main(){ syso( SubClass.value) //SuperClass sc[] =new SuperClass[10] //syso(ConstantClass.HW);}} Output: super init 123 Only the class that directly defines this field will be initialized for access to static fields. The subclass will not be initialized. The first comment is cancelled and runs without any output. The reference class through array definition will not trigger the initialization of the class, but will initialize an initialization of a subclass that is automatically generated by the virtual machine and inherited from the object class. This class represents an array. The properties and methods of the array are implemented in this class (length attributes and clone()). There will be no output after the second comment is cancelled. Constant will be placed in the constant pool during compilation. ConstantClass.HW is placed in the constant pool of the NotInitialization class during compilation. NotInitialization does not have a symbolic reference to NotInitialization.
The above is all about this article's brief discussion on active and passive citations to classes in Java, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!