What is the runtime class of an object ?

R

Razvan

Hi !






Static synchronized methods are synchronizing on the runtime class of
an object. From the official documentation:

getClass()
public final Class getClass()Returns the runtime class of an object.
That Class object is the object that is locked by static synchronized
methods of the represented class.

Returns:
the object of type Class that represents the runtime class of the
object.


So, this runtime class object is a hidden object that is created only
when there is a static and synchronized method ? Aside from
synchronizing static methods I see no use for this object. Is it true
that this object is dedicated to synchronization ?



Regards,
Razvan
 
M

Michael Borgwardt

Razvan said:
Static synchronized methods are synchronizing on the runtime class of
an object. From the official documentation:

getClass()
public final Class getClass()Returns the runtime class of an object.
That Class object is the object that is locked by static synchronized
methods of the represented class.

Returns:
the object of type Class that represents the runtime class of the
object.


So, this runtime class object is a hidden object that is created only
when there is a static and synchronized method ? Aside from
synchronizing static methods I see no use for this object. Is it true
that this object is dedicated to synchronization ?

No. I think you're completely misunderstanding the API docs. The class
object in question is *always* present for every class that is loaded.
Since it is unique for each class, it is also just the right thing to
be used by static (i.e. per-class) code to synchronize on, so the
language designers choose to use it that way for the 'synchronized'
method modifier.
 
T

Tor Iver Wilhelmsen

So, this runtime class object is a hidden object that is created only
when there is a static and synchronized method ?

No; it's the "metaclass" object (instance of java.lang.Class)
associated with the class. It's available at all times as long as the
class is loaded. Can also be obtained using Class.forName() or the
automagic reference SomeType.class.
Aside from synchronizing static methods I see no use for this
object. Is it true that this object is dedicated to synchronization
?

No. Take a look at its class (java.lang.Class) for a host of methods
used for e.g. reflection/introspection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top