Three ways to load the class java class loading

1, to create a new instance of a class key (static load)
in a method of loading a new run-time
, such as: Dog dog = new Dog () ;

2, calling the Class.forName () method
by loading reflection type, and creates an object instance
as: Class = clazz the Class.forName ( "Dog");
Object Dog clazz.newInstance = ();

3, a call ClassLoader instance loadClass () method
() method loadClass loaded by the ClassLoader instance. Applications can implement your own class loader through inheritance ClassLoader.
Such as: Class = clazz ClassLoader.loadClass ( "Dog");
Object Dog clazz.newInstance = ();

Three differences:

1 and 2 are used by the same class loader are current class loader. (Ie: this.getClass.getClassLoader).
3 designated by the user class loader. If you need to find the class other than the current class path, the use of only three ways. A third way to load the current class and classes belong to different namespaces.
Also:
1 static load, dynamic load 2,3

Two exceptions (exception):

  • When the static loaded if the operating environment is not found to be initialized class, it is thrown NoClassDefFoundError, it is an anomaly in the system Error in JAVA

  • When dynamically loaded state if the operating environment is not found to be initialized class, is thrown ClassNotFoundException, it is abnormal in JAVA system is a checked exception

Class.forName and ClassLoader.loadClass difference

Class load comprises three steps: loading (loading), the connection (link), the initialization (the initialize)
the Class.forName (className) is actually called Class.forName (className, true, this.getClass ( ) getClassLoader (). ). The second parameter refers to the Class is loading is not to be initialized.
ClassLoader.loadClass (className) actually calls ClassLoader.loadClass (name, false), the second parameter refers to whether the Class link.
Class.forName (className) loaded class has been initialized, and ClassLoader.loadClass (className) class has not been loaded link. In general, these two methods the same effects can be loaded Class. But if the program is initialized depend on whether the Class, you must use Class.forName (name) up.
For example, JDBC, often see such usage, Class.forName ( "com.mysql.jdbc.Driver").
If you replace the getClass (). GetClassLoader (). LoadClass ( "com.mysql.jdbc. Driver "), can not.
The source code com.mysql.jdbc.Driver:
// Ourselves the Register with the DriverManager The
static {
the try {
java.sql.DriverManager.registerDriver (new new Driver ());
The catch} (SQLException E) {
the throw a RuntimeException new new ( "Can Not Register Driver!");
}
}
Turned, Driver in the static block will register itself java.sql.DriverManager. The static block is to be performed in the initializing Class.
So this place can only use Class.forName (className).

For the same class, JVM will load at most once. But if the same class file is loaded into a different ClassLoader, then after loading two classes it is completely different. Since the class has been loaded by the class loader Examples of such combinations identifying the full path name of the class. Provided packagename.A Class, are a class loader loads CL1 and CL2, the system has two different java.lang.Class Example: <CL1, packagename.A> and <CL2, packagename.A>.

classload process
https://www.cnblogs.com/gdpuzxs/p/7044963.html
write https://blog.csdn.net/javazejian/article/details/73413292# own class loader
after jar package import item, start classnotfound:
priority at the highest level with the back -jar jar package. If the -jar option is specified, all environment variables and command line developed by the search path will be ignored. JVM APPClassloader will only to jar package for search
https://www.cnblogs.com/zpbolgs/p/7267384.html
https://blog.csdn.net/sayyy/article/details/81120749
3, MANIFEST file
https : //www.jb51.net/article/131101.htm

Guess you like

Origin blog.csdn.net/qq_30242987/article/details/88571383