code example of Java class loading mechanism

Source: Internet
Author: User

 PackageTypeInfo;ImportJava.util.Random;classinitable{Static Final intStaticfinal = 47 ; Static Final intStaticFinal2 = RandomInit.rand.nextInt (1000) ; Static{System.out.println ("Initializing Initable"); }}classinitable2{Static intStaticnonfinal = 147 ; Static{System.out.println ("Initializing Initable2"); }}classinitable3{Static intStaticnonfinal = 47 ; Static{System.out.println ("Initializing Initable3"); }}classrandominit{ Public StaticRandom Rand =NewRandom (47) ; Static{System.out.println ("Hello"); }}classinitable4{Static{System.out.println ("Initable 4"); }    Static Final Doublex =Classinitialization.pi;}classinitable5{Static{System.out.println ("Initable 5"); }} Public classclassinitialization{Static Final DoublePI = 3.1415 ;  Public Static voidMain (string[] args)throwsException {System.out.println (initable4.x); //because X is a compile-time (constant-expression), it does not cause Initable4 initializationSYSTEM.OUT.PRINTLN ("----"); Class initable= Initable.class;//does not belong to one of the 4 conditions that cause initialization, does not cause initializationSystem.out.println ("After creating initable ref") ; System.out.println (initable.staticfinal);//Staticfinal is a compile-time constant that is replaced with 47 when parsing, and does not cause initializationSystem.out.println (INITABLE.STATICFINAL2);//StaticFinal2 is not a compile-time constant, it causes initialization, and belongs to GetstaticSystem.out.println (initable2.staticnonfinal);//very volume, which causes initialization, belongs to GetstaticClass initable3 = Class.forName ("TypeInfo. Initable3 ");//will cause initialization, but don't know whySystem.out.println ("After creating Initable3 ref");        System.out.println (initable3.staticnonfinal); Initable5[] Initable5s=NewINITABLE5[10];//does not belong to that 4 in one of the conditions, not initialized    }}

classssclass{Static{System.out.println ("Ssclass"); }}classSuperclassextendsssclass{Static{System.out.println ("Superclass init!"); }     Public Static intValue = 123;  PublicSuperclass () {System.out.println ("Init Superclass"); }}classSubclassextendssuperclass{Static{System.out.println ("Subclass Init"); }    Static intA;  PublicSubclass () {System.out.println ("Init Subclass"); }} Public classnotinitation{ Public Static voidMain (string[] args) {System.out.println (subclass.value);//because the subclass refers to a static member of the parent class, it does not belong to the four of the initialization, so the subclass is not initialized    }}/*** Ssclass * Superclass init! * 123*/
 Public classstatictest{ Public Static voidMain (string[] args) {staticfunction (); }    StaticStatictest st =Newstatictest (); Static{System.out.println ("1"); } {System.out.println ("2"); } statictest () {System.out.println ("3"); System.out.println ("A=" +a+ ", b=" +b); }     Public Static voidstaticfunction () {System.out.println ("4"); }    inta=110; Static intb =112;}/*** Use Invokestatic (staticfunction ()), so to load and initialize the statictest * Initialize, load, validate, and then prepare (at this stage, St is assigned a value of null,b to 0, then parse ( This instance does nothing at all) * Finally performs initialization, forming the Cinit as follows * {* St = new Statictest (); * SYSTEM.OUT.PRINTLN ("1"); * b = 112; *} * only  To connect the stage (validate, prepare, parse) once, in fact, the entire class can be used, in the execution of new statictest, the member variables are initialized * to 0 or null, that is, a=0, and then follow the initialization of non-static code blocks and member variables in order to initialize, that is, to execute the first * {* SYSTEM.OUT.PRINTLN ("2"); *} * then execute a=110;     * Last Execution Statictest () * {* SYSTEM.OUT.PRINTLN ("3"); * SYSTEM.OUT.PRINTLN ("a=" +a+ "b=" +b); * } * * So the overall execution output is * 2 * 3 * a=110,b=0 * 1 * 4*/


Thinking in Java Notes

code example of Java class loading mechanism

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: 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.