Java inertia Initialization

Source: Internet
Author: User

Initialization effectively achieves as much inertia as possible. Only the. Class syntax is used to obtain the reference to the class. Initialization is not triggered. However, to generate class references, class. forname ()

Initialize immediately

If a static final value is a constant during the compilation period, this value can be obtained without class initialization. However, if a field is set to static or final, it is not enough.

To ensure this behavior, access to it will force class initialization, because it is not a compilation constant

If a static domain is not final, when accessing it, it always requires her to first link (allocate storage space for this domain) and initialize (initialize) before being read.

This bucket)

The preparation for using classes involves three steps:

1. load. This is executed by the class loader. This step searches for bytecode (usually in the path specified by classpath, but this is not required) and creates a class object from these bytecode.

2. Link: In the link phase, the bytecode of the class will be verified to allocate storage space for the static domain. If necessary, all references to other classes created by the class will be parsed.

3. Initialization. If this class has a superclass, initialize it, initialize the static domain and static initialization block.

Initialization is delayed until the static method (the constructor is implicitly static) or the very few static fields are referenced for the first time.

/*****/Package COM. runtech; import Java. util. random; public class demo002 {public static random = new random (47); public static void main (string [] ARGs) {Class C1 = initable1.class; // system is not initialized. out. println (initable1.staticfinal01); // The System of the compiling constant. out. println (initable1.staticfinal02); // enforces class initialization, which is a non-compile-time constant system. out. println ("-----------------------------------"); try {Class C2 = Class. forname ("com. runtech. initable1 "); // The system is initialized directly. out. println (initable2.staticfinal01); system. out. println (initable2.staticfinal02);} catch (classnotfoundexception e) {e. printstacktrace ();} system. out. println ("---------------------------------"); Class C3 = initable3.class; system. out. println (initable3.staticfinal01); // non-compile-time constant system. out. println (initable3.staticfinal02); system. out. println ("---------------------------------"); New initable2 (); system. out. println (initable2.staticfinal01); system. out. println (initable2.staticfinal02) ;}} class initable1 {static final int staticfinal01 = 47; static final int staticfinal02 = demo002.random. nextint (1000); static {system. out. println ("initable1 static");} public initable1 () {system. out. println ("initable1 constructor") ;}} class initable2 {static int staticfinal01 = 12; static final int staticfinal02 = demo002.random. nextint (1000); static {system. out. println ("initable2 static");} public initable2 () {system. out. println ("initable2 constructor") ;}} class initable3 {static int staticfinal01 = 12; static final int staticfinal02 = demo002.random. nextint (1000); static {system. out. println ("initable3 static");} public initable3 () {system. out. println ("initable3 constructor ");}}

Output:

47
Initable1 static
258
-----------------------------------
Initable2 static
12
555
-----------------------------------
Initable3 static
12
693
-----------------------------------
Initable2 Constructor
12
555

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.