When I create new object say Employee emp = new Employee
, then per mine understanding system class loader(in case of stand alone program) or web class loader(in case of webserver
like tomcat) loads the Employee class object(including class level fields/methods) and then actual Employee instance is created.
My question is why class object is garbage collected even if reference is dead but object is garbage collected if unreachable.
Is it because Employee class object is still referred internally by class loader(system/web class loader) but employee instance is not referred by class loader ?
Update :- My question has been marked duplicate of When and how are classes garbage collected in Java? but it does not actually answer mine question. This question is mainly about comparison why class is not GC'ed but object is not so it should be reopened . That link just answered that when/when not class object can be garbage collected but my question then why not object also. Is it not reference by class loader ?