Start with two questions:
1. If a class has multiple subclass objects, will the parent class objects be one or more new subclass objects?
2. Does each type have a constant pool or does each object have its own constant pool?
Can you answer the question quickly? After understanding the principles of the Java Virtual Machine Method area, these two problems are very clear.
The Java Virtual Machine divides the memory into a method area, heap, and thread stack. To put it simply, the method area is used to store type metadata information. A class file is a representation of a class before it is used by a Java Virtual Machine. Once this class is used, the Java Virtual Machine will load, connect (verify, prepare, and parse) it and initialize it. The result of loading (which involves the related theory of the Class Loader) is to transform from the. Class file to a specific data structure in the method area. This data structure stores the following information:
Type information
Full qualified name of this type
Full qualified name of direct superclass of this type
Is this type A class type or an interface type?
This type of access modifier
List of fully qualified names of any direct superinterface
Field Information
Field name
Field Type
Field Modifier
Method Information
Method Name
Method return type
Number and type of method parameters (in order)
Method Modifier
All class (static) variables except Constants
One pointClassloader pointer
A pointer to a Class Object
Type constant pool (the second problem at the beginning is returned here): constant data and symbol reference for other types.
The metadata of each class accesses the metadata in the method area, whether it is to construct an instance of the class or call a method of an object of the class.
When constructing an object, JVM will allocate space to the object in the heap, these spaces are used to store the attributes of the current object instance and the instance attributes of its parent class (and these attributes are obtained from the method area). Note, this is not just about allocating space for the instance attributes of the current object, but also assigning the instance attributes of the parent class. Now we can answer the first question, that is, when a subclass of the parent class is instantiated, JVM also constructs an object of the parent class. This problem can also be confirmed from another perspective: when calling the constructor of the current class, the constructor of its parent class will be called first until the object is called, and the call of constructor means the creation of the instance, therefore, when subclass is instantiated, the parent class will certainly be instantiated.