Class initialization and class loading issues

Source: Internet
Author: User
Tags reflection

JVM Process Terminated:

1. The program runs to the last normal termination

2. The program runs to the use of system.exit () or Runtime.getruntime (). Exit ()

3. End of an unhandled exception or error encountered during program execution

4. The flat of the program terminates the JVM process

Look at the program below.

Class A

public Class A 
{public
	static int a =5;
}
Execute first
public class Test1 
{public
	static void Main (string[] args) 
	{
		A A = new A ();
		a.a++;
		System.out.println (A.A);
	}

In the implementation
public class Test2 
{public
	static void Main (string[] args) 
	{
		a b = new A ();
		System.out.println (B.A);
	}


The result of the Test1 is 6 Test2, the result is 5.

Because two run Java programs are in different JVM processes, data is not shared between two JVMs


Several scenarios for loading classes

1. Load a class file from a local file system

2. Load the class file from the jar package

3. Load class files over the network

4. Dynamically compile a Java source file and perform the load


Three stages of a class connection

Validation Readiness Resolution


Initialization of a class

1. The initial value specified when declaring a static property

2. Use static initialization to specify an initial value for a static property

If this class has not yet been loaded and connected, the program loads and joins the class first

If the direct parent class of the class has not yet been initialized, initialize its direct parent class first

If there are initialization statements in the class, the system executes the initialization statements sequentially

When a program actively uses any class, the system guarantees that the class and all of its parent classes (both direct and indirect) will be initialized.


Java initializes classes or interfaces in the following six ways

1. Create an instance of a class, create an instance for a class by creating an instance using the new operator, creating an instance from the reflection mechanism, and creating an instance by reflection

2. Call a static method of a class

3. Accessing a static property of a class or interface, or assigning a value to the static property

4. Use reflection to force the creation of a class or interface corresponding to the Java.lang.class object

5. Initializes a subclass of a class when a program actively uses any class, the system guarantees that the class and all of its parent classes, both direct and indirect, will be initialized.

6. Run a main class directly using the Java.exe command, and when you run a main class, the program initializes the main class




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.