Java-deep Java Virtual Machine 3

Source: Internet
Author: User

The initialization time of the class

 Packagepractise;classparent{Static intA =3; Static{System.out.println ("Parent static Block"); }}classChildextendsparent{Static intb = 4; Static{System.out.println ("Child Static Block"); }} Public classTest4 {Static{System.out.println ("Test4 static Block"); }     Public Static voidMain (string[] args) {System.out.println (child.b); }}

Result:
TEST4 static block
Parent static block
Child static Block
4
 Packagepractise;classparent5{Static intA =3; Static{System.out.println ("PARENT5 static Block"); }}classChild5extendsparent5{Static intb = 4; Static{System.out.println ("Child5 static Block"); }} Public classTEST5 {Static{System.out.println ("Test5 static Block"); }     Public Static voidMain (string[] args) {Parent5 parent=NewParent5 ();        System.out.println (PARENT5.A);            System.out.println (child5.b); }}

Result:
TEST5 static block
PARENT5 static block
3
CHILD5 static block
4

The "active use" of a child class in a program causes the parent class to be initialized, but the "active" use of the parent class does not cause the child class
Initialization

Calling the LoadClass method of the ClassLoader class to load a class is not an active use of the class and does not cause the initialization of the class.

 Packagepractise;classc{Static{System.out.println ("Class C"); }} Public classTest6 { Public Static voidMain (string[] args) {ClassLoader loader=Classloader.getsystemclassloader (); Try{Class<?> CLZ = Loader.loadclass ("practise. C); System.out.println ("----------------"); CLZ= Class.forName ("practise. C); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Result:
----------------
Class C

Java-deep Java Virtual Machine 3

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.