博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java类class forName()方法及示例
阅读量:2529 次
发布时间:2019-05-11

本文共 2224 字,大约阅读时间需要 7 分钟。

类类forName()方法 (Class class forName() method)

  • forName() method is available in java.lang package.

    forName()方法在java.lang包中可用。

  • forName() method is used to return the class object for the Class with the given class_name.

    forName()方法用于返回具有给定class_name的Class的类对象。

  • forName() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get any error.

    forName()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出现任何错误。

  • forName() method may throw an exception at the time of returning a Class object.

    forName()方法在返回Class对象时可能会引发异常。

    • LinkageError: This exception may throw when we get linking error.LinkageError :当我们获得链接错误时,可能会抛出此异常。
    • ExceptionInInitializeError: In this exception when the initialization is done by this method fails.ExceptionInInitializeError :在此方法中,初始化失败的此异常。
    • ClassNotFoundException: In this exception when the given class does not exist.ClassNotFoundException :如果给定的类不存在,则在此异常中。

Syntax:

句法:

public static Class forName(String class_name);

Parameter(s):

参数:

  • String class_name – represents the fully qualified name of the given class.

    字符串class_name –代表给定类的完全限定名称。

Return value:

返回值:

The return type of this method is Class, it returns this Class object for the class with the given name.

该方法的返回类型为Class ,它将为具有给定名称的类返回此Class对象。

Example:

例:

// Java program to demonstrate the example // of Class forName (String class_name) method of Class public class ForNameOfClass {
public static void main(String[] args) throws Exception {
// It returns the Class 'java.lang.Object' object for the class // with the given class name Class cl = Class.forName("java.lang.Object"); // Display Name, Package and Interfaces System.out.print("Class 'java.lang.Object' Name: "); System.out.println(cl.getName()); System.out.print("Class 'java.lang.Object' Package: "); System.out.println(cl.getPackage()); System.out.print("Class 'java.lang.Object' Interface: "); System.out.println(cl.getInterfaces()); }}

Output

输出量

Class 'java.lang.Object' Name: java.lang.ObjectClass 'java.lang.Object' Package: package java.langClass 'java.lang.Object' Interface: [Ljava.lang.Class;@68f7aae2

翻译自:

转载地址:http://zgxzd.baihongyu.com/

你可能感兴趣的文章
python学习---字符串
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
简单的栈和队列
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
点语法
查看>>
IO之Socket网络编程
查看>>
PCRE demo【转】
查看>>
矩阵的SVD分解
查看>>
gitlab的配置
查看>>
linux访问ftp服务器命令
查看>>
ActiveMQ学习笔记之异常
查看>>
LuoguP4012 深海机器人问题(费用流)
查看>>
自动机
查看>>