您当前的位置:首页 > 互联网教程

java中 this.getClass().getCLassLoder()是什么意思

发布时间:2025-05-24 10:39:17    发布人:远客网络

java中 this.getClass().getCLassLoder()是什么意思

一、java中 this.getClass().getCLassLoder()是什么意思

1、getClass():取得当前对象所属的Class对象

2、getClassLoader():取得该Class对象的类装载器

3、类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题哪里,通过它可以得到一个文件的输入流

4、Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.

5、the object of type Class that represents the runtime class of the object.

6、public ClassLoader getClassLoader()

7、Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

8、If a security manager is present, and the caller´s class loader is not null and the caller´s class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested, then this method calls the security manager´s checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it´s ok to access the class loader for the class.

9、If this object represents a primitive type or void, null is returned.

10、the class loader that loaded the class or interface represented by this object.

11、SecurityException- if a security manager exists and its checkPermission method denies access to the class loader for the class.

12、ClassLoader, SecurityManager.checkPermission(java.security.Permission), RuntimePermission

13、Class.getClassLoader()的一个小陷阱:)

14、昨天我的code总在Integer.class.getClassLoader().getResource("*********");这一句抛出空指针异常,定位为getClassLoader()返回null,查了一下jdk的文档,原来这里还有一个陷阱:

15、jdk中关于getClassLoader()的描述:

16、* Returns the class loader for the class. Some implementations may use

17、* null to represent the bootstrap class loader. This method will return

18、* null in such implementations if this class was loaded by the bootstrap

19、*<p> If a security manager is present, and the caller's class loader is

20、* not null and the caller's class loader is not the same as or an ancestor of

21、* the class loader for the class whose class loader is requested, then

22、* this method calls the security manager's<code>checkPermission</code>

23、* method with a<code>RuntimePermission("getClassLoader")</code>

24、* permission to ensure it's ok to access the class loader for the class.

25、* represents a primitive type or void, null is returned.

26、上面的英文可以用下面的话来理解:

27、装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器,当然这除了测试JVM外,我想不出还有其他的用途。你应该已经发现到了这样一个问题,类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?没错,确实存在这样的根,它就是神龙见首不见尾的Bootstrap ClassLoader.为什么说它神龙见首不见尾呢,因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,伟大吧。在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类),这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader()来获得,我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。还是先前假定的情况,当Java中出现新的类,AppClassLoader首先在类传递给它的父类类装载器,也就是Extion ClassLoader,询问它是否能够装载该类,如果能,那AppClassLoader就不干这活了,同样Extion ClassLoader在装载时,也会先问问它的父类装载器。我们可以看出类装载器实际上是一个树状的结构图,每个类装载器有自己的父亲,类装载器在装载类时,总是先让自己的父类装载器装载(多么尊敬长辈),如果父类装载器无法装载该类时,自己就会动手装载,如果它也装载不了,那么对不起,它会大喊一声:Exception,class not found。有必要提一句,当由直接使用类路径装载器装载类失败抛出的是NoClassDefFoundException异常。如果使用自定义的类装载器loadClass方法或者ClassLoader的findSystemClass方法装载类,如果你不去刻意改变,那么抛出的是ClassNotFoundException。

28、这里jdk告诉我们:如果一个类是通过bootstrap载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 new Object().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader("this.getClass().getClassLoader()“),这样一来就不会有问题。

二、Java学习笔记:关于.getClass()和.class的区别

在深入探讨Java中getClass()和.class的区别之前,先简要理解Java的反射机制。反射允许在运行时获取和修改类、接口、字段和方法等信息,为编写更加动态和灵活的代码提供了可能。

具体来说,getClass()方法是对象实例所独有的,用于获取对象的类型类(Class对象)。而.class关键字则用于获取类的类型类,前提是已经知道类的全限定名。例如,通过A.class可以获取A类的类型类,进而获取A类的所有属性和方法。

接下来,详细阐述getClass()和.class在使用场景上的差异与功能:

-通过对象实例调用getClass()方法,可以获取该实例所属类的类型类,如a.getClass()返回的是A类的类型类A.class。

-使用类名直接拼接.class可以获得该类的类型类,如A.class,适用于已知类名的场景。

-使用类型类对象调用各种方法,可获取类的详细信息,如类名、直接父类、接口实现、是否为数组、是否为基本类型等。

-这些信息在编写通用代码、实现动态类加载、处理序列化等场景中非常有用。

-类型类对象还支持类型转换,如将类型类转换为某个子类的类型类。

在Java反射中,getClass()和.class之间最大的区别在于获取类型的方式和使用场景。前者适用于对象实例,后者适用于已知类名。同时,了解这两种方式如何在实际编程中应用,可以帮助开发者编写更灵活、可扩展的代码。理解它们的区别有助于在需要动态获取类型信息的场景中做出恰当的选择。

三、java中getClass().getName()有什么作用

1、getClass返回的是Class对象,这个对象用来描述你现在所使用的对象的类信息,比如有

2、将返回a对象的信息描述,当然这些信息都是动态的,比如a对象的某个属性有什么值阿等等,你可以在程序运行时通过写代码动态的获得这些信息。getClass().getName()是用来返回Class对象所代表的具体对象的名称。



相关内容FAQs: