java - Unable to understand Class object -


oracle java documentation on intrinsic locks , synchronization says:

you might wonder happens when static synchronized method invoked, since static method associated class, not object. in case, thread acquires intrinsic lock class object associated class. access class's static fields controlled lock that's distinct lock instance of class.

i did not understand concept of class object. after studding online content know:

a class object sort of meta object describing class of object such name, package etc.

my questions are:

  1. when created?
  2. is garbage collected @ point of time?
  3. as used synchronized static method, mean there 1 instance of class object per jvm?

there similar question what class object(java.lang.class) in java. doesn't answer questions.

[update]

a new question added in comment section of answer provided manouti mentioned there can multiple instance of class object:

  1. is there chance static synchronized method can accessed multiple thread simultaneously if multiple instance of class object exists?

1. when created?

it created when class loaded jvm using classloader. class loaded when referenced other class. classloader typically creates class instance when calling classloader#loadclass(string classname). explained in this link java language specification:

loading refers process of finding binary form of class or interface type particular name, perhaps computing on fly, more typically retrieving binary representation computed source code java compiler, , constructing, binary form, class object represent class or interface.

2. garbage collected @ point of time?

just other instance, if class instance no longer reachable, eligible gc. happens when no object of type represented class instance reachable, , classloader loaded class not reachable well.

3. used synchronized static method, mean there 1 instance of class object per jvm?

not necessarily. if define custom classloader, have 2 instances of class. in scenario, may classcastexception if try convert object of class a "same type" a if loaded 2 different classloaders.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -