The life cycle of the Java class

Source: Internet
Author: User

1. Loading
We write a Java source file, compiled into a file with a suffix called. Class, which combines a four-byte code file,
The Java Virtual machine recognizes this kind of file, and the Java life cycle is the process of loading the class file from the load to the extinction.
About loading, in fact, is to find the class file of the source file to load the information into the method area,
A Java.lang.Class object is then instantiated in the heap area as the entry for the information in this class in the method area.
But this functionality is implemented outside the JVM, mainly because it is convenient for the application to decide how to get the class itself.
The way in which different virtual machines are implemented is not necessarily the same, the hotspot virtual machine is used when it is loaded in a way that
There are other pre-loaded first.
2. Connect
Typically, it intersects the load phase and initialization phase, and the process consists of three parts: validation, preparation, and parsing three steps
(1) Verification: Determine whether the class conforms to the Java language Specification, there is no duplication of attributes and behavior, inheritance is reasonable, in short, is to ensure that the JVM can execute
(2) Preparation: The main thing is to allocate memory for member variables modified by static and set default initial values
The default initial values are as follows:

1. Eight basic data types the default initial value is 0
2. Default initial value of reference type is NULL
3. A static final modifier is assigned directly, for example: static final int x=10; the default is 10.
(3) Resolution: This phase of the task is to convert the symbolic reference in the constant pool into a direct reference, it is plain that the JVM will all the class or interface name, field name, method name into a specific memory address.
3. Initialization
This stage is the process of assigning static variables (class variables), that is, only static modifiers can be initialized, and the order of execution is:

The parent class static domain or static code block, then the subclass static domain or subclass static code block
4. Use
There are still three steps in the use of a class: Object instantiation, Garbage collection, object finalization

(1) Object instantiation: Is the execution of the contents of the constructor in the class, if the class exists the parent class JVM will execute the parent class constructor by showing or implicitly, in heap memory for the parent class's instance variable space, and give the default initial value, The actual value is then given to the instance variable itself based on the code content of the constructor, and then the reference variable gets the first address of the object, invoking the instance variables and methods by manipulating the object
(2) Garbage collection: When the object is no longer referenced, it will be marked with a special garbage mark on the virtual machine, waiting for GC recycle in the heap.
(3) End of object: After the object is recycled by GC, the object will no longer exist, and the life of the object will come to an end.
5. Class Unloading
That is, the life cycle of the class goes to the last step, the program no longer has a reference to the class, the class will be garbage collected by the JVM, from the end of life ...

Package Com.etc.test;class a{static int a;//class variable string name;int id;//static code block static{a=10; System.out.println ("This is the static code block of the parent class" +a);} Construct code block {id=11; System.out.println ("This is the construction code block ID of the parent class:" +id);} A () {System.out.println ("This is the non-parametric constructor of the parent class");} A (String name) {System.out.println ("This is the name of the parent class" +name);}} Class B extends a{string name;static int b;static{b=12; System.out.println ("This is a static block of code for subclasses" +b);} B (String name) {super (); this.name = name; System.out.println ("This is the name of the subclass:" +name);}} public class Test666 {public static void main (string[] args) {b bb=new B ("GG");}}

  

The results of the output are as follows:

This is a static code block for the parent class 10
This is a static code block for subclasses 12
This is the construction code block of the parent class Id:11
This is the non-parametric constructor of the parent class
This is a subclass of Name:gg.

The life cycle of the Java class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.