A detailed analysis of the loading and object creation processes of the Java Basics class

Source: Internet
Author: User

I believe that when we interview Java there will always be some companies to do written questions, and Java class loading and object creation process of knowledge points is also one of the common topics. The following is an example of detailed analysis.

Instance problems

Instance Code

Parent class
1 package mytest.javabase; 2  3 public class Parent {4     int a = ten; 5     static int b = one; 6     //Static code block 7     static {8         SYSTEM.OUT.PR INTLN ("Parent Static code block: b=" + b); 9         b++;10     }11     //code block     {         System.out.println ("Parent code block: a=" + a);         "Parent code block: b=" + b);         b++;16         a++;17     }18     //parameterless constructor     , parent () {         System.out.println ("Parent parameterless constructor: a=" + a);         System.out.println ("Parent parameterless constructor: b=" + b);     }24     // The parameter constructor is     the parent (int a) {         System.out.println ("Parent has a parameter constructor: a=" + a);         System.out.println (" The parent has a parameter constructor: b= "+ B",     }30     //method,     void function () {         System.out.println ("Parent function Run ... ");     }35 36}
Child class
1 package mytest.javabase; 2  3 public class child extends Parent {4     int x = ten; 5     static int y = one; 6     //Static code block 7     static {8
   
    SYSTEM.OUT.PRINTLN ("Child Static code block: y=" + y); 9         y++;10     }11     //code block     {         System.out.println ("Child code block: x=" + x);         System.out.println (" Child code block: y= "+ y",         y++;16         x++;17     }18     //Constructor (     ) {         System.out.println (" Child constructor: x= "+ x",         System.out.println ("Child constructor: y=" + y),     }24     //method     , void function () {27         System.out.println ("Child function Run ...");     29 30}
   
Test class
1 package mytest.javabase; 2  3 public class Test {4 publicly     static void main (string[] args) {5 child         demo = new Child (); 6         Demo.func tion (); 7 System.out.println ("..... ..... ... ...). ..... ... ..... ..... ..... ..... ..... ..... ...         .. ..... ....... .....???????? ..... ..... ........... ........ ........................ ...");.. ..... ... "); 8 Child Child         = new Child (); 9         child.function ();     }11}

We can not look at the results of the operation, our own thinking, run the results will be what, and then compare the results of their own thinking is the same.

Run results
123456789101112131415161718192021 Parent静态代码块:b=11Child静态代码块:y=11Parent代码块: a=10Parent代码块: b=12Parent无参构造函数: a=11Parent无参构造函数: b=13Child代码块: x=10Child代码块: y=12Child构造函数: x=11Child构造函数: y=13Child function run ………………………………………………………………………………………………………………………………Parent代码块: a=10Parent代码块: b=13Parent无参构造函数: a=11Parent无参构造函数: b=14Child代码块: x=10Child代码块: y=13Child构造函数: x=11Child构造函数: y=14Child function run …… 
Results detailed analysis

We run the main method of the test class

1, start the JVM, start allocating memory space;

2, start loading the Test.class file, load into the method area, the static content in the process of loading to enter the static zone;

3, at the beginning to run the main method, then the JVM will call the main to the stack run, start from the first line of the method to run down;

4 . In the Main method new child (), then the JVM will find in the method area there is no child file, If you do not load the Child.class file, and child inherits the parent class, you are also looking for the parent class, and if you do not also want to load the Parent.class file.

5. All non-static content in Child.class and Parent.class is loaded into a non-static zone, and static content is loaded into the static zone. During static loading, static member variables are loaded before static code blocks are loaded, and then static member methods are loaded.

Description: The load for a class is executed only once. The next time you create an object, you can get the class information directly in the method area.

6 . Start the default initialization for all static member variables in the static zone. After the default initialization is complete, the initialization is displayed for all static member variables.

7 . All static member variables show that after initialization is complete, the execution of the static code block begins. Executes the static code block of the parent class first, and then executes the static code block of the subclass.

123 //这时输出Parent静态代码块:b=11Child静态代码块:y=11

Description:>> Static code blocks are executed when the class is loaded, and the load of the class is executed only once, so the static code block is only executed once;

>> code in non-static code blocks and constructors is executed at object creation time, so object creation (new) is performed once.

8 . The Parent.class file and the Child.class file load are completed.

9 . Start creating child objects in the heap. Allocating memory space to a child object is actually allocating memory addresses.

10 . Start the default initialization of non-static member variables in the class.

11, start loading the corresponding construction method, perform implicit three-step

① has an implicit super (); ② display initialization (to all non-static member variables) ③ the code in the constructor of this class is executed after the code block has been constructed

Super () is the constructor for calling the parent class, which is the constructor for parent, and there is an implicit three-step in the parent's constructor: first Super (), then the display initialization of the parent, and then the non-static construction code block of the parent. Finally executes the code in the parent's constructor.

12345 //这时输出Parent代码块: a=10Parent代码块: b=12Parent无参构造函数: a=11Parent无参构造函数: b=13 

Note: Although the parent does not write extends, we know that there is a superclass object in Java, which is the parent class of all classes, so the super () of the parent class here is the constructor that invokes object

After the execution of the parent, it comes back to the second step in the implicit three steps of child's own: Display initialization, then execute the code of the child's non-static code block, and finally execute the child's constructor

12345 //这时输出Child代码块: x=10Child代码块: y=12Child构造函数: x=11Child构造函数: y=13

12, the object creation is completed, the memory address is assigned to the demo use.

13, the implementation of the Demo.function () method.

12 //这时输出Child function run ……

14, because later created a new child object, so repeat the "9" after the steps, it is easy to understand that its output is

123456789 Parent代码块: a=10Parent代码块: b=13Parent无参构造函数: a=11Parent无参构造函数: b=14Child代码块: x=10Child代码块: y=13Child构造函数: x=11Child构造函数: y=14Child function run ……

Simple drawing of a memory run example diagram

Summarize

We know that when we create an object, we go to the JVM's method area to get information about the class of the object, and if there is no information for that class in the method area, then we need to load it in and load it, so that we can create an object with the information of that class.

In general, when the Java class is compiled, a class file is generated, and the class file is loaded into the Java Virtual machine JVM at run time, and the class file is loaded by the ClassLoader. In the JVM (which should be exactly in the JVM's method area), a meta-information object describing the class structure is formed, through which the structure information of class is known: such as constructors, properties, methods, and so on.

First, the loading process of the class

First, when the JVM executes, when it encounters a new class, it goes to the in-memory method area to find the class information, and if it finds it, it takes it directly, and if it does not, it loads the class file into the method area. When a class is loaded, static member variables are loaded into the static region of the method area, and non-static member variables are loaded into the non-static zone of the method area.

Static code blocks are code that executes automatically when a class loads, and non-static code blocks are code that executes automatically when an object is created, and does not create objects that do not execute non-static blocks of code for that class.

Order: Static code block-"non-static code block-" class construction method.

Loading process:

1. The JVM will first go to the method area to find a. Class that does not have the corresponding classes present. If so, use it directly; if not, load the. CLSS of the related class into the method area.

2 . When loading the. Class into the method area, load the parent class and load the subclasses first, load the static content, and then load the non-static content

3. Loading static content:

    • Load all static content in. class into a static region under the method area
    • Default initialization of all static variables after static content loading is complete
    • After the default initialization of all static variables is complete, the explicit initialization
    • Executes a static block of code when all static variables under the static zone are explicitly initialized

4 . Loading non-static content: Loads all non-static variables and non-static code blocks in the. Class into a non-static zone under the method area.

5, after the execution, the entire class load is complete.

Both static and non-static methods are passive calls, that is, the system does not invoke execution automatically, so the user does not invoke the execution, the main difference is that the static method can be directly called by the class name (instantiation of the object can also), and not static method can only be called before the object can be instantiated.

Second, the creation of the object process

1. When a new object is created, it creates a space in the heap memory.

2 . Assign an address to the open space.

3 . Load all non-static members of the object into the open space.

4 . After all non-static members are loaded, the default initialization is done for all non-static member variables.

5 . After the default initialization of all non-static member variables is complete, the constructor is called.

6, when the construction function into the stack execution, divided into two parts: first execute the implicit three steps in the constructor, and then execute the code written in the constructor function.

Implicit three-step:
① Execute super () statement

③ executing construction Code blocks

7, after the entire constructor execution and the stack, the space allocation of the address assigned to the Reference object.

Third, other Super Statement, the following three scenarios may occur:

1) The first line of the construction method body is the this statement, then the implicit three steps are not performed, but instead the constructor method corresponding to the this statement is called, and the first row is certainly not constructed by the this statement.

1234567891011121314151617181920212223 packagemytest.javaBase;publicclassStudent {    privateString name;    privateString age;    Student() {    };    Student(String name) {        this.name = name;    };    Student(String name, String age) {        // 不会执行隐式三步        this(name);        this.age = age;    };}

2) The first line of the constructor method body is the super statement, the constructor method of the corresponding parent class is called,
3) The first line of the constructor method body is neither the this statement nor the super statement, it is implicitly called super (), which is the default constructor of its parent class, which is why a parent class typically provides a default constructor method;

More information: http://www.cnblogs.com/study-everyday/

A detailed analysis of the loading and object creation processes of the Java Basics 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.