ClassLoader类resolveClass()方法 (ClassLoader Class resolveClass() method)
resolveClass() method is available in java.lang package.
resolveClass()方法在java.lang包中可用。
resolveClass() method is used to link the specified class let suppose when class A has been linked already then in that case this method simply returns.
假设已经链接了类A时,可使用resolveClass()方法链接指定的类,那么在这种情况下,此方法只是返回。
resolveClass() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
resolveClass()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
resolveClass() method may throw an exception at the time of linking class.
resolveClass()方法在链接类时可能会引发异常。
NullPointerException: In this exception when the given class is null.
NullPointerException :在给定的类为null的情况下,此异常。
Syntax:
句法:
protected void resolveClass(Class cl_name);
Parameter(s):
参数:
Class cl_name – represents the name of the class to link.
类cl_name –表示要链接的类的名称。
Return value:
返回值:
The return type of this method is void, it returns nothing.
此方法的返回类型为void ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example
// of void resolveClass(Class cl_name) method of ClassLoader
public class ResolveClass extends ClassLoader {
ResolveClass(Class cl1) {
// By using super keyword to call parent class
// Constructor
super.resolveClass(cl1);
// Display Resolve Class Name
System.out.println("Class Name: " + cl1.getSimpleName());
}
public static void main(String[] args) throws Exception {
// We are parsing Class to ClassLoader constructor
Class cl = Class.forName("java.lang.String");
// Calling ResolveClass Constructor
ResolveClass rc = new ResolveClass(cl);
}
}
Output
输出量
Class Name: String
翻译自: https://www.includehelp.com/java/classloader-resolveclass-method-with-example.aspx