简体   繁体   English

class.class代表什么

[英]What does class.class represents

when we synchronize some static method lock is acquired on class.class so what does class.class represents, by googling and reading through some forums i came to know its instance of java.lang.Class and for all the classes in memory there exists one instance of java.lang.Class associated with them. 当我们同步class.class上的一些静态方法锁时,class.class代表什么,通过在某些论坛上进行谷歌搜索和阅读,我知道了它的java.lang.Class实例,并且内存中的所有类都存在一个与它们关联的java.lang.Class的实例。 is it?? 是吗??

One can create object of that class with this and acquire lock on this, apart from that what all major functionalities does it offer which are useful in my regular java application?? 可以使用该类创建该类的对象并获得对此的锁定,此外,它提供了所有对我的常规Java应用程序有用的主要功能?

any reference link is welcomed. 欢迎任何参考链接。 Thanks!! 谢谢!!

...its instance of java.lang.Class and for all the classes in memory there exists one instance of java.lang.Class associated with them. ...它的java.lang.Class实例,并且内存中的所有类都存在一个与之关联的java.lang.Class实例。

Basically, yes, although I think it's one copy per classloader rather than in total. 基本上是的,尽管我认为每个类加载器一个副本,而不是总计一个副本。 But as you normally have only one classloader... 但是因为您通常只有一个类加载器...

The Class instance represents the class as a whole, as distinct from other classes, just as instances of classes are distinct from all other instances of that class. Class实例代表整个类,与其他类不同,就像类的实例与该类的所有其他实例是不同的一样。 So since static methods are specific to the class (just like non-static methods are specific to the instance), it makes sense to synchronize on the class instance when calling a static method (if the method requires synchronization). 因此,由于静态方法特定于类(就像非静态方法特定于实例一样),所以在调用静态方法时(如果该方法需要同步)在类实例上进行同步是有意义的。 Which is why that's what the JLS says happens if you apply the synchronized keyword to the method as a whole. 这就是为什么这是,如果你的应用是什么JLS说发生synchronized关键字的方法作为一个整体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM