ways to load classes of virtual machines
1. Create an instance of a class from the New keyword
Loading with the new method at run time
such as: Dog Dog = new Dog ();
2. Call the Class.forName () method
Load the type by reflection and create an object instance
such as: Class Clazz = Class.forName ("Dog");
Object dog =clazz.newinstance ();
3, call a ClassLoader instance of the LoadClass () method
Loaded by the LoadClass () method of the ClassLoader instance. An application can implement its own class loader by inheriting ClassLoader.
such as: Class Clazz = Classloader.loadclass ("Dog");
Object dog =clazz.newinstance ();
The difference between three people:
The class loaders used by 1 and 2 are the same and are all current classloader. (ie: this.getClass.getClassLoader). 3 Specifies the class loader by the user. If you need to find a class outside the current classpath, you can only use the 3rd method. The 3rd way to load a class is a different namespace than the current class. In addition, 1 is static loading, 2, 3 is dynamic loading
Two exceptions (Exception)
When statically loaded, if the class to initialize is not found in the running environment, the Noclassdeffounderror is thrown, which is an error in the Java exception System.
When the dynamic state is loaded, if the class to initialize is not found in the running environment, the classnotfoundexception is thrown, which is a checked exception in the Java exception System.
the difference between Class.forName and Classloader.loadclass
Class loading consists of 3 steps: Load (loading), connect (link), Initialize (initialize).
Class.forName (ClassName) is actually called Class.forName (ClassName, True, This.getclass (). getClassLoader ()). The second parameter refers to whether the class must be initialized after being loading.
Classloader.loadclass (className) actually calls Classloader.loadclass (name, false), and the second argument refers to whether the class is link.
Class.forName (ClassName) loaded class has been initialized, and Classloader.loadclass (ClassName) loaded class is not yet link. In general, the two methods have the same effect, and can load class. However, if the program relies on whether class is initialized, it must use Class.forName (name).
For example, in JDBC programming, it is common to see this usage, class.forname ("com. MySQL.jdbc.Driver ").
If replaced by GetClass (). getClassLoader (). LoadClass ("Com.mysql.jdbc.Driver"), No.
Com.mysql.jdbc.Driver's source code is as follows:
Register ourselves with the DriverManager
static {
try {
Java.sql.DriverManager.registerDriver (New Driver ());
catch (SQLException E) {
throw new RuntimeException ("Can ' t Register driver!");
}
}
Originally, driver in the static block will register themselves to Java.sql.DriverManager. And the static block is executed in the initialization of class.
So this place can only use Class.forName (ClassName).
For the same class, the JVM is loaded at most once. But if the same class file is loaded with a different classloader, then the two classes that are loaded are completely different. Because the class that has been loaded is identified by a combination of the class loader instance of the class and the full path name of the class. Equipped with PackageName. Class A, which is loaded by the ClassLoader CL1 and CL2 respectively, so there are two different Java.lang.Class instances in the system: <CL1, PackageName. A> and <cl2, PackageName. A>.
Turn from: http://blog.csdn.net/zhangzeyuaaa/article/details/12706241