The detailed process for initializing Java objects __java

Source: Internet
Author: User
the process of initializing a class and its objects

First, when you need to initialize a class

-> Dog Dog = new Dog () when an object is first created;
-> Dog.staticfields When you first access a static method or static field of a class;

The Java interpreter will find the path to the class and locate the Dog.class file that has been compiled.

Ii. access to the resources of the class

The JVM then loads the Dog.class and generates a class object. This time, if there is a static method or variable, the static initialization action will be executed. It's time to note that static initialization runs only once when the class object is loaded for the first time during the program's operation. These resources will be placed in the JVM's method area.

Method Area:
-also called the static area, like the heap, is shared by all threads.
-The method area contains all the elements that are always unique throughout the program, including all class and static variables.

Iii. Initialization object->dog Dog = new Dog ()

1. The first time you create a dog object, you first perform the one or two steps above
2. Allocate enough storage space on the heap for the dog object, all properties and methods are set to the default value (the number is 0, the character is null, the Boolean is false, and all references are set to null)
3. Perform constructors to check for a parent class, and if a parent class invokes the constructor of the parent class, assuming that dog does not have a parent class, the assignment of the default value field is the initialization action of the method.
4. Perform the constructor function. initialization in the case of a parent class

Suppose dog extends Animal

To perform the first step, find the Dog.class file, and then discover in the loading process that he has a base class (through the extends keyword), then perform the 12th step of the animal class, load its static variables and methods, and then load the static variables and methods of the subclass dog after the load is finished.
If the animal class has a parent class and so on, the final base class is called the basic class.

PS: Because static initialization of subclasses may depend on the static resources of the parent class, you first load the parent class's statically resources.

Next you want to new dog object, first allocating storage space for the dog object-> to dog constructor-> creating the default attribute. Here the first line of its constructor has an implied super (), the parent class constructor, so it jumps to the constructor of the parent class animal.

Java will help us complete the constructor complement, dog the actual implicit constructor is as follows

Dog () {
To create default properties and methods
Call the constructor of the parent class super () (can be explicitly written out)
Assign and initialize the default properties and methods separately
}

The parent class animal also allocates storage space before executing the constructor-> to its constructor-> create the default attribute-> found I have no parent class, this time give it the default property assignment and method initialization.

Then execute the remainder of the constructor, and then jump to the constructor of the subclass dog.

The subclass dog assigns and initializes the default properties and methods, and then completes the next section of the constructor.

First, why does the constructor of the parent class animal continue the properties and method assignment of the subclass dog.
-Because the initialization of dog variables and methods of subclasses is possible to use the properties or methods of their parent class animal. So the subclass constructs the default properties and methods and does not assign the value, but jumps to the constructor of the parent class to complete the construction of the parent class object before initializing its own properties and methods.
-This is why the constructor of the subclass shows the reason to force write on the first line when the parent class constructor super () is invoked, and the program needs to jump to the parent class constructor to complete the construction of the parent class object before executing the remainder of the subclass constructor.

Second, why the properties and methods are initialized before executing the other parts of the constructor.
--because the explicit part of the constructor may use the properties and methods of the object.

Tips: In fact, this initialization process is to ensure that after the resource initialization used in front of the object has been initialized. Very powerful, the father who worships Java.

Let's say so many examples .

Note here that the main function is also a static resource, and the main function of the dog class is to invoke the static resource of the dog.

Parent class Animal class Animal {/*8, execute initialization/private int i = 9;  

protected int J; /*7, invoking the constructor method, creating default properties and methods, and finding that they do not have a parent/public Animal () {/*9, executing the remainder of the constructor method, and then returning to the subclass constructor. System.out.println ("  
        i = "+ i +", j = "+ J");  
     j = 39;  
    /*2, initialize static objects and static methods of the base class/private static int x1 = Print ("Static animal.x1 initialized");  
        static int print (String s) {System.out.println (s);  
    Return 47;  

}//Subclass Dog public class Dog extends Animal {/*10, initialize default properties and methods * * Private int k = print ("dog.k initialized"); 
     /*6, start creating objects, that is, allocating storage space-> creating default properties and methods.
     * Encountered an implicit or explicitly written out super () constructor that jumps to the parent class animal.  
        * SUPER () to write in the first line of the constructor/public Dog () {/*11, initialization end execution remaining statements/System.out.println ("k =" + K);  
    System.out.println ("j =" + j);

Static object static method of/*3, initialization subclass, of course Mian function is also static method * * private static int x2 = Print ("Static dog.x2 initialized"); /*1, to execute static main, first to load Dog.class file, loading process found that the parent class animal, * so also to load Animal.classFile until you find the base class, this is animal*/public static void Main (string[] args) {/*4, after the previous step completes the main method, the OUTPUT statement */System.ou 
T.println ("Dog constructor");   
/*5, encounters the new Dog (), invokes the constructor of the Dog object */Dog Dog = new Dog (); 
    /*12, run the remainder of the main function of the program * * SYSTEM.OUT.PRINTLN ("Main left");   }  
}

Guess the results for yourself, the output is:

Static animal.x1 initialized
Static dog.x2 initialized
Dog Constructor
Animal:animal.i = 9, ANIMAL.J = 0
DOG.K initialized
DOG:DOG.K = 47
DOG:ANIMAL.J = 39
Main Left

Attention Ah, good to read do not understand asked me, a little complicated, the clarity is very clear, good night

Related Article

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.