Suppose you have a cat class named:
1, even if the static keyword is not displayed, the constructor is actually a static method. So when you create an object of type cat, or the static method/static domain of the Cat class is first accessed (referenced), the Java interpreter must look for the classpath (find on Classpath) to locate the Cat.class file.
2, and then the class loader loads the Cat.class, and all actions related to static initialization are performed. Also, static initialization occurs only once when the class object is first loaded (the keyword is static, is bound to the class, and is accessible through the class, and the object of that class shares the resource.) )
3. When you create an object with new Cat (), you first allocate enough storage space on the heap for the Cat object.
4. This storage space is zeroed, which automatically sets all the basic type data in the Cat object to the default value (the number is 0, the Boolean and the character type are the same), and the reference is set to NULL.
5. Perform all initialization actions that appear in the field definition. ( initialized in the order of the statement positions )
6, the execution of the constructor.
Public static void Main (String args[])
This is the entry for the Java program execution and is a static method. When the sentence is executed, the interpreter will look for the modified. class file and load it in (load the parent class, reload the interface, etc.), initialize the static variables of the class first (in the Order of the Code), then initialize the non-static reference object, Finally executes the constructor.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java object creation process (simple and straightforward)