I really do mean identity-equality here.
For example, will the following always print true?
System.out.println("foo".getClass() == "fum".getClass());
getClass() method returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
The Java Object getClass() method returns the class name of the object.
The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object.
You cannot override getClass .
Yes, class tokens are unique (for any given classloader, that is).
I.e. you will always get a reference to the same physical object within the same classloader realm. However, a different classloader will load a different class token, in conjunction with the fact that the same class definition is deemed different when loaded by two distinct classloaders.
See this earlier answer of mine for a demonstration of this.
For two instances of class X
,
x1.getClass() == x2.getClass()
only if
x1.getClass().getClassLoader() == x2.getClass().getClassLoader()
Note: Class.getClassLoader()
may return null which implies the bootstrap ClassLoader.
Yes.
The returned Class object is the object that is locked by static synchronized methods of the represented class.
If it was possible to return multiple instances, then
public static synchronized void doSomething() {..}
would not be thread-safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With