The process of initializing Java classes with objects (a classic interview question)

Source: Internet
Author: User

This article no longer interprets these issues in a classloader perspective.

First of all, Java code has a feature that member variables can be used in the previous method, defined later. This feature, many people say Java is great, but why? Why is Java able to do this?

Let's first look at an interview question:

Write the result of the above code. In fact, for Java to understand the more deeply, disdain to solve the problem, because read the code to write the results of the run, and then the general but this problem, if written, it is really not easy, because, we want to look at this problem in a similar C language perspective. OK, we first understand a basic concept, memory space must be applied first, then initialized, then used. For example, malloc allocates space in the C language, and then initializes write 0 and then uses. In fact, so is Java. First, we start with the main method, with only one row of the new object (line 26th). The new object first loads the class, which is for sure, so "test" needs to load the class, which is the bytecode file that loads the test into the method area (the permanent generation). The "Static" part of the class needs to be processed. In other words, the static part is entered into the permanent generation with the bytecode file of test. The process is to apply all the static parts to the space first, and then assign the initial value from top to bottom for each static member. There are two initial values, one is the default initial value of Java for the base data type, this default initial value is given to each member when the space is applied, and the Java programmer uses "=" To assign the initial value to the member. , five static members are first allocated memory in a permanent generation. At this point, k=0,t1=null,t2=null,i=0,n=0. After the completion of the space application, we assign 0 to K, although K is allocated after memory is 0, but still have to assign 0 to K, because "=" to the right of "0" is the programmer's assignment to K. Then is to give the T1 instance an object, that is, T1 is originally null, now the new object is assigned to T1, so we will build a test object. When you build an object, you initialize all of the non-static parts into the heap memory. At this point, you understand the Java syntax, why the static member is "class name. Member", and non-static member is "object. Member". Because the owning relationship is different. So we start looking for non-static members, with J and a building block. So it is to apply the space to J first and then run the print ("J") method to give the return value of the method to J. So, the first paragraph of the program printed the results: print: Thereafter, k=1, N=1, I=1. Then there is the non-static part, only the construction block, therefore: Thereafter, k=2, n=2, i=2. At this point, we have finished preparing the object, that is, the non-static code is executed, so the object is instantiated, the Java instantiation of the object using the construction method, so do: print: Thereafter, T1 is no longer null, and then initialize T2, the process and T1, So the result of the operation is: then initialize the static I, so it is done: output printing: Then initialize N (line 6th), assign N to a value of 99 directly. But nothing is printed. And then down is the static block: Output printing: At this point, all the static parts are initialized, you can new Test ("init"): Output printing: So the overall print as follows: summary, in fact, Java itself is also the code from the top down, but the static and non-static parts in two of the elements. Java members have allocated space, assign the default value of two procedures, and first for all members to apply for space, and then from top to bottom each assignment.

The Java class and object initialization process (a classic interview question)

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.