Class executes three steps before execution: load-and-connect initialization
Loading of classes in 1.java
Java class loading refers to reading the binary data in the class's. class file into memory, placing it in the method area of the run-time data area, and then creating a class object in the heap to encapsulate the data structure of the class in the method area. The class of the heap area can be understood as a mirror of the method area, corresponding to the data structure of the class of the method area, and the invocation of classes is realized through this mirror.
There are several ways to load a. class file:
1. Loading directly from the local system
2. Download the. class file over the network
3. Download the. class file from Zip,jar
4. Extract the. class file from a proprietary database
5. Dynamically compile the Java source file into a. class file
The end result of class loading is to generate a class object that is located in the heap, which encapsulates the data structure of the class within the method area, and provides the Java programmer with an interface to access the data structures within the method area
View source The class object can only be created by a Java virtual machine and its constructors are privatized
2. Connect
1. Verify: Ensure that the class being loaded is correct (primarily to prevent the disgusting class file from being loaded)
2. Prepare: Allocate memory for static variables of the class and initialize them to default values
3. Parsing: Converting a conforming reference in a class to a direct reference
3. Initialization
Assigning the correct initial value to a static variable of a class
All Java Virtual machine implementations must be initialized when each class or interface is "first actively used" by a Java program.
Java's use of classes is divided into: active use, passive use
Active use of six kinds: (except for these 6 kinds, the other is passive use)
1. To create an instance of a class
2. Access static variables for a class or interface or assign a value to the static variable
3. To invoke a static method of a class
4. Reflection
5. Initializes subclasses of a class
6. Classes that are labeled to start classes when the Java Virtual machine is started
4. Class Loader ClassLoader
Class loader type
1.java class loader with virtual machine:
-----Root Class Loader Bootstrap
The ClassLoader does not have a parent classloader, it is responsible for loading the virtual machine's core class library, such as java.lang.String, and so on, the root classloader is used to load the class when the JVM is started, so that the JVM can work properly, so it is implemented with native (c + +) code, was first created, at the lowest level. It does not inherit the Java.lang.ClassLoader class.
-----Extension Class Loader extension
The class loader's parent class loader is the root class loader. It gets the load class library from the directory specified by the Java.ext.dirs system properties or loads the class library from the Jre\lib\ext subdirectory of the JDK's installation directory. If the jar is placed in this directory, it will also be automatically loaded with the extension classloader. The extension ClassLoader is a Java class and is a subclass of the Java.lang.ClassLoader class
-----system ClassLoader
Also becomes the application ClassLoader, its parent classloader is the extension classloader, it will load the directory and jar files configured in Classpath, it is the default parent classloader of the user-defined ClassLoader, the system ClassLoader is the Java class, the subclass of the Java.lang.ClassLoader class
2. User-defined class loader: is a subclass of Java.lang.ClassLoader, you can define the class loader
Class loader mechanism
The class loader is used to load the class into the Java virtual machine, and the class loading process uses the Father's delegation mechanism , which can ensure the security of the Java platform, because the user-defined class loader cannot load the reliable class that should be loaded by the parent loader in this mechanism. such as java.lang.String is always loaded by the root ClassLoader. In this delegation mechanism, in addition to the root classloader of Java, the rest of the loaders have only one parent loader.
Custom Class Loaders
The user-defined class loader needs to satisfy the parent delegate mechanism, which is the default system ClassLoader for its parent. To implement a custom loader, you only need to integrate the ClassLoader class and then overwrite the Findclass (String name) method, which specifies the name of the load class according to the parameter, and returns a reference to the corresponding class object
Custom class Loader class: Myclassloader
Package com.longpo.classloader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Public class Myclassloader extends ClassLoader {
private String name; //class loader name
private String path="d:\\"; //Load the path of the class
Private final String type=". Class"; //class file extension
Public Myclassloader (String name)
{
super ();
This.name=name;
}
//Specify parent class loader
Public Myclassloader (ClassLoader parent, String name)
{
super (parent); //display Specifies the loader's parent loader
This.name=name;
}
//Set the path of the load class
Public String GetPath () {
return path;
}
public void SetPath (String path) {
This.path = path;
}
@Override
Public String toString () {
return "Myclassloader:" + name;
}
//Read binary data of the corresponding class file---Simple read here
private byte[] loadClassData (String name)
{
//Find the corresponding file according to the class name to be loaded
Name=name.replace (".", "\ \"); The Com.longpo.test directory structure is \ \
File file=new file (Path+name+type);
//Create byte array based on file size
byte[]bytes=New byte[(int) file.length ()];
try {
InputStream is = new FileInputStream (file);
int Len=is.read (bytes); //Returns the length of the read byte
Is.close ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return bytes;
}
//Be sure to override this method
@Override
Public class<?> Findclass (String name) throws ClassNotFoundException {
//Get binary data for class file
byte[]data=loadclassdata (name);
return this.defineclass (name, data, 0,data.length); Returns a reference to a class object
}
}
Define a Test load class: Simple
Package com.longpo.classloader;
Public class Simple {
Public Simple ()
{
System.out.println ("Simple class loader name is:" +This.getclass (). getClassLoader ());
}
}
Testing a custom load class: Test
Package com.longpo.classloader;
Public class Test {
public static void Main (string[] args) throws exception{
//loader1 's parent loader defaults to the system loader--plus the system loader, the extension loader, the root loader
Myclassloader loader1=New Myclassloader ("Loader1");
Loader1.setpath ("d:\\lib\\loader1\\"); Loader1 Load Path
//loader2 's parent loader is loader1--with loader1 loader, system loader, extension loader, root loader
Myclassloader loader2=New Myclassloader (Loader1,"Loader2");
Loader2.setpath ("d:\\lib\\loader2\\"); Loader2 Load Path
//loader3 's parent loader is NULL, which is the root loader--only the root loader above
Myclassloader loader3=New Myclassloader (null,"Loader3");
Loader3.setpath ("d:\\lib\\loader3\\"); Loader3 Load Path
Test (LOADER2);
Test (LOADER3);
}
public static void Test (ClassLoader loader)throws exception{
Class Clazz=loader.loadclass ("com.longpo.classloader.Simple"); Loadclas will automatically call the Findclass method
Object object=clazz.newinstance ();
}
}
At this point, the compiled class file is sent to the folder of the corresponding loader directory, I use Eclipse, you can find the. class file in the bin directory of the project's directory.
Run test at Eclipse to get the result:
The relationship of each loader is
When using Loader2 to load simple, according to the Father delegation mechanism, will start from the root loader to try to load, continue to load, the way the System class loader loaded successfully. Use Loader3 to load simple, according to the Father delegation mechanism, first loaded by the root loader, loading failed to have loader3 themselves loaded
Original Address http://techfoxbbs.com/blog-1-2.html
class loader for Java ClassLoader