On Active and Passive References to Classes in Java

  • 2021-01-19 22:16:45
  • OfStack

This paper focuses on active and passive referencing of classes in Java. The details are as follows.

Active referencing. Here are five scenarios for active referencing

1. When a class is not initialized, it will be initialized, create an object, read or set a static field, and call a static method.
2, reflection
3. The parent class is initialized before the child class is initialized
A class containing the main method that is initialized when the virtual machine starts
5. Dynamic language support with jdk (unknown)

Passive reference:


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 For a static field visit only directly define the field class will be initialized, subclass will not initialize, cancel the first comments after operation, there is no output, by defining reference classes will not trigger a class array initialization, but 1 automatically generated by the virtual machine will initialize the subclasses of initialization, inherited from the object class class - this class represents an array, the array of properties and methods are implemented in this class (length attributes and clone ()), there will be no output after second remove comments, constant will be put into the constant pool at compile time, ConstantClass.HW is placed in the constant pool of class NotInitialization at compile time. NotInitialization does not have a symbolic reference to NotInitialization.

conclusion

That is all about active and passive referencing of classes in Java. I hope it will be helpful to you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: