From Ian Utting, I.A. [email protected]
Before main can be called a number of steps must be followed:
--The class must be loaded
--The class must be linked, which includes:
--Verification
--Preparation
--Resolution
--The class (including any superclasses) must be initialised
Class Loading
n A class is retrieved by name from a class file (or elsewhere) by a Class Loader
n A class loader may cache or pre-fetch class definitions, but any errors must occur only when the class is used in the program
n ClassFormatError , UnsupportedClassVersionError , NoClassDefFoundError or ClassCircularityError are possible
{
java.lang.ClassLoader
n {Each class has an associated class loader which was used to load it, and will be used to load any class loaded by it. It can be retrieved by calling: getClass().getClassLoader()
n The system has a bootstrap class loader. ClassLoader.getSystemClassLoader() will return it.
n When a class needs to be loaded, the system calls the loadClass(String name) method on the class loader.
n So a class is defined by the pair of its name and its class loader.
Defining you own class loader
n Create a new ClassLoader sub-class to get classes from another source, or in another format by overriding findClass(String name) or loadClass(String name)
n Use this.getParent() to retrieve your parent’s class loader and delegate class loading to it.
n Use defineClass(String name, byte [] b) to turn a byte array containing a class file into a Class object
n Use resolveClass(Class c) to link the resulting Class object
}
Class Linking: verification
n Verification ensures that the class file which is a candidate for loading is structurally correct, i.e.:
n Every opcode is valid, and all the operands referred to have the correct type for the opcode
n Every branch instruction must refer to the start of another instruction in the same method
n All indexes (of methods, invocations etc.) must refer to valid areas of the constant pool
n The depth of the operand stack must never exceed the value given in the method information in the class file
n Etc
Class Linking: preparation and resolution
n Preparation creates the static fields for the class in the runtime constant pool, and sets them to their default initial values.
n Resolution resolves symbolic references to classes (etc.) from this class to other (already loaded) classes known to this class loader, or loads them recursively.
n Access privileges are checked (e.g. private is enforced)
Class Initialisation
n Class initialisation is triggered by creating a new instance of the class, or by referencing a static field or method
n Initialisation causes static fields of the class to be set to their declared values, and static intitialiser blocks to be invoked in the order in which they were declared.
class A {
static {
/* initialisation code */
}
}
----------------------------------------------------------------------------------------------------------------------------------
Conclusion :
1 . Creating new instance of the class :
· Static variables (initialization with default value, or execute related method for static variables)
· Static fields
· Constructor
· Main method
2 . Only reference a static field or method :
No other static variables initialization.
Example :
package JavaPuzzles.javaInitial;
/**
* output :
Test1:t1
print:j
print:constructor
Test1:t2
print:j
print:constructor
print:i
print:static fields
This is static main field
initialization: 1)2)3) 4)
*/
public class Test2 {
public static int k = 0;
//1)
public static Test2 t1 = new Test2( "t1" );
//2)
public static Test2 t2 = new Test2( "t2" );
//3)
public static int i = print ( "i" );
public static int n = 99;
public int j ;
static {
print ( "static fields " );
}
public Test2(String str ) {
System. out .println( "Test1:" + str );
j = print ( "j" );
print ( "constructor" );
}
public static int print(String str) {
System. out .println( "print:" + str );
return 0;
}
public static void main(String[] args){
//4)
System. out .println( "This is static main field" );
}
}