Static variables are not garbage collected?


Contents of page >
  • Static variables/ fields are not garbage collected in java?
  • Can Static variables be garbage collected in some scenario?
  • But why Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected?


Static variables/ fields are not garbage collected in java?
Yes.


Can Static variables be garbage collected in some scenario?
Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected in java.


But why Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected?
Because static variables are referenced by the Class objects which are referenced by ClassLoaders. So, Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected in java.


Summary -
So in this tutorial we learned Static variables are not garbage collected in java.




Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


Related>

Acquiring lock on class, 2 Ways to acquire lock on class


Difference between object Lock and class Lock



No comments:

Post a Comment