View RSS Feed

Core Java

Managing Initialization and Class Loading

Rate this Entry
by , 11-29-2011 at 03:20 AM (1374 Views)
Java avoids many of the problems found with other languages related to the startup and initialization of objects in the right order, by taking a different approach to loading. The key to this is that everything in Java is an object. Each class exists in its own separate file. Therefore files are not loaded until the code is needed. The expression of this is that “class code is loaded at the point of first use.” This is usually when the first object of that class is constructed, but loading also occurs when a static field or static method is accessed.

Static initialization always takes place at the point of first use. All the static objects and the static code block will be initialized in the order that they are defined in the class definition when they are loaded. Statics are initialized only once. In the example below, I will demonstrate the initiation process showing both static initialization and inheritance.

Java Code:
import static com.acme.examples.Print.*;

public class MyBase {
	private int i = 9; protected int j; 
	MyBase() {
		print("i = " + i + ", j = " + j); 
		j = 39;
		} 
	
	private static int x1 =
		printInit("static MyBase.x1 initialized"); 
		static int printInit(String s) {
			print(s);
			return 47;
		}
}


import static com.acme.examples.Print.*;

public class MyExtended extends MyBase {
	private int k = printInit("MyExtended.k initialized");

	public MyExtended() {
		print("k = " + k);
		print("j = " + j);
	}

	private static int x2 = printInit("static MyExtended.x2 initialized");

	public static void main(String[] args) {
		print("MyExtended constructor");
		MyExtended myE = new MyExtended();
	}
}
When you launch MyExtended the first method that will be accessed is MyExtended.main( ) (a static method), so the loader goes out and finds the compiled code for the MyExtended class (in the file MyExtended.class). While loading it, the loader realizes that it has a base class (MyBase), which it then loads. This will happen irrespective of whether you will make an object of the base class. The next part is the static initialization in the root base class (MyBase) is performed, and then any static initialization in the derived class. This is because the derived-class static initialization could depend on the base class member being initialized.

When all the necessary classes have been loaded, the object can be created. The order is the following; 1. All the primitives in this object are set to their default values; 2. Object references are set to null; 3. The base-class constructor is called (Either automatically, or by calling the constructor); 4. The base class construction goes through the same process in the same order as the derived-class constructor; 5. After the base-class constructor completes, the instance variables are initialized in textual order. 6. The rest of the body of the constructor is executed.

Submit "Managing Initialization and Class Loading" to Facebook Submit "Managing Initialization and Class Loading" to Digg Submit "Managing Initialization and Class Loading" to del.icio.us Submit "Managing Initialization and Class Loading" to StumbleUpon Submit "Managing Initialization and Class Loading" to Google

Comments