Initialization and class loading

Source: Internet
Author: User

Because everything in Java is an object, many activities become simpler, and this is one example. The code for each object in Java exists in a separate file. The file is not loaded unless you really need the code. In general, we can assume that unless an object of that class is constructed, the code is not actually loaded. When loaded, all the static and static blocks are initialized in their original order (i.e., the order in which they are written in the class definition code). Of course, thestatic data is initialized only once.

1. initialization of inheritance

We need to be aware of the entire initialization process, including inheritance, and have a holistic concept of what is going on in this process. Please

Observe the following code:

:  beetle.java// the full process of initialization.class insect {   int i = 9;  int j;  insect ()  {  prt ("I  =  " + i + ", j =  " + j);  j = 39;   }  static int x1 =  prt ("static insect.x1 initialized ");   static int prt (String s)  {  system.out.println (s);   Return 47;  }}public class beetle extends insect {  int  k = prt ("beetle.k initialized");   beetle ()  {  prt ("k =   " + k"   prt ("j = "  + j);  }  static  Int x2 =  prt ("static beetle.x2 initialized");  static int  PRT (string s)  { &nbsP System.out.println (s);  return 63;  }  public static void  Main (String[] args)  {  prt ("Beetle constructor");   beetle b =  new beetle ();  }}  the output of the program is as follows: static insect.x initializedstatic beetle.x  initializedbeetle constructori = 9, j = 0beetle.k initializedk =  63j = 39

RightBeetleRunJava, the first thing that happens is that the loader is outside to find that class. During the load process, the loader notes that it has a base class (that is,extendsthe keyword to express), so it is loaded into it. This process occurs regardless of whether you are preparing to build an object of that base class. If the underlying class contains another base class, the other base class is also loaded, and so on. Next, in the root base class (this is theInsect) ExecutionStaticinitialized, then executed in the next derivative class, and so on. It is important to ensure that the order is initialized because the initialization of the derived class may depend on the correct initialization of the underlying class member. At this point, the necessary classes are all loaded, so the object can be created. First, all the basic data types in this object are set to their default values, and the object handle is set toNULL. The underlying class builder is then called. In this case, the call is made automatically. But it can be used completely .Superfrom the row specifies the builder call (as if theBeetle ()the first action in the builder). The construction of the base class takes the same process as the derived class builder. After the foundation is completed, the instance variables are initialized in the order in which they were originally built. Finally, execute the remaining body part of the builder.

Executes from main program main() and, if there is a extends , starts from the beginning of the parent class and executes static on initialization of the class. Statement, and then executes the constructor method, which, if it is a subclass, executes the Super() method and finally loads the other program.


Initialization and class loading

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.