Unable to understand Class object
Asked Answered
A

2

7

Oracle Java documentation on Intrinsic Locks and Synchronization says:

You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.

I did not completely understand the concept of Class object. After studding some online content I get to know:

A Class object is sort of a meta object describing the class of an object such as name, package etc.

My questions are:

  1. When is it created?
  2. Is it Garbage Collected at some point of time?
  3. As it is used by synchronized static method, does it mean there will be only one instance of Class object per JVM?

There is a similar question what is Class Object(java.lang.class) in java. But it doesn't answer my questions.

[Update]

A new question is added in the comment section of the answer provided by manouti as he mentioned there can be multiple instance of Class object:

  1. Is there any chance that a static synchronized method can be accessed by multiple thread simultaneously if multiple instance of Class object exists?
Abednego answered 3/6, 2015 at 20:20 Comment(1)
You can't have 2 loaded instances of the same class objectDashtikavir
B
9

1. When is it created?

It is created when the class is loaded by the JVM using a classloader. A class is loaded when it is referenced by some other class. A ClassLoader typically creates this Class instance when calling ClassLoader#loadClass(String className). This is explained in this link from the Java Language Specification:

Loading refers to the process of finding the binary form of a class or interface type with a particular name, perhaps by computing it on the fly, but more typically by retrieving a binary representation previously computed from source code by a Java compiler, and constructing, from that binary form, a Class object to represent the class or interface.

2. Is it Garbage Collected at some point of time?

Just like any other instance, if the Class instance is no longer reachable, it is eligible for GC. This happens when no object of the type represented by the Class instance is reachable, and the classloader that loaded the class is not reachable as well.

3. As it is used by synchronized static method, does it mean there will be only one instance of Class object per JVM?

Not necessarily. If you define a custom classloader, then you could have two instances of a Class. In this scenario, you may even get a ClassCastException if you try to convert an object that is of some class A to the "same type" A if they were loaded by two different classloaders.

Brassard answered 3/6, 2015 at 20:34 Comment(3)
Nicely explained +1. One question regarding last point: Then is there there any chance that a static synchronized method can be accessed by multiple thread simultaneously if multiple instance of Class object exists?Abednego
Just an additional note about Class object collection, the JLS (docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.7) specify that classes loaded by the bootstrap classloader may not be unloaded, Class unloading is seen as a memory optimization but in some cases class loading could be a costly operation and it could make sense to disable Class unloading completely (e.g. embedded environment with very slow storage)Rosenwald
@Abednego Yes if the two threads lock on Class instance loaded separately by two "malicious" classloaders. Normally though (when you're not messing with classloaders), it shouldn't be the case: the two threads would lock on the same Class instance.Brassard
P
1
  1. From the JavaDocs on Class:

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

  1. As long as instances of a class are in use and references to the Class object persist, it will stay in memory.

  2. Yep. Class is immutable so there's no real synchronization concern there.

Puritanical answered 3/6, 2015 at 20:28 Comment(2)
Does it mean when first instance of any class is created, an instance of Class will be created automatically by JVM. And if we create more objects of same class, no other Class object will be created? And will be eligible for Garbage Collection when the last living instance is Garbage Collected? I guess you didn't get my last question.Abednego
@Kartic, Yes, If you have Foobar f1 = new Foobar(); and Foobar f2 == new Foobar(), then f1.getClass() and f2.getClass() will both always return the same object reference.Eshman

© 2022 - 2024 — McMap. All rights reserved.