Java.lang.Class.forName (String name, Boolean initialize, ClassLoader loader) method

Source: Internet
Author: User

Describe

The Java.lang.Class.forName (string name, Boolean initialize, ClassLoader loader) method returns a class object with the given string name, Use the given class loader.

The class or interface that is loaded by the specified class loader. If the parameter loader loader is empty, the class is loaded by booting the class loader. When the initialization of the class is initialized, the initialize parameter is true if it is not initialized.

Statement

The following is a declaration of the Java.lang.Class.forName () method

[Java]View Plain copy
      1. Public static class<?> forname (String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException
Parameters
    • name -This is the fully qualified name of the desired class.

    • Initialize -This indicates whether the class must be initialized.

    • Loader -This is the class loader for classes that must be loaded.

return value

This method returns the class object of the desired class.

Abnormal
    • Linkageerror --if linkage fails.

    • Exceptionininitializererror --if the initialization caused by this method fails.

    • classnotfoundexception --If the class cannot be located by the specified class loader.

Instance

The following example shows how to use the Java.lang.Class.forName () method.

[Java]View Plain copy
  1. Import java.lang.*;
  2. Public class Classdemo {
  3. public static void Main (string[] args) {
  4. try {
  5. Class cls = Class.forName ("Classdemo");
  6. //Returns the ClassLoader object
  7. ClassLoader Cloader = Cls.getclassloader ();
  8. / * Returns the class object associated with the class or interface
  9. With the given string name, using the given ClassLoader. */
  10. Class cls2 = Class.forName ("Java.lang.Thread", true, Cloader);
  11. //Returns the name of the class
  12. System.out.println ("Class =" + Cls.getname ());
  13. System.out.println ("Class =" + Cls2.getname ());
  14. }
  15. catch (ClassNotFoundException ex) {
  16. System.out.println (Ex.tostring ());
  17. }
  18. }
  19. }

Let's compile and run the above program, which will produce the following results:

[Java]View Plain copy
    1. Class = Classdemo
    2. Class = Java.lang.Thread

Java.lang.Class.forName (String name, Boolean initialize, ClassLoader loader) method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.