Does static variables get garbage collected?

Does static variables get garbage collected?

Ninja Asked on 18th September 2018 in Java.
Add Comment
1 Answer(s)
Best answer

No, Static variables cannot be elected for garbage collection. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.

Static variables are referenced by Class objects which are referenced by Class loaders – so unless the ClassLoader itself becomes eligible for collection the static variables won’t be collected.

Ninja Answered on 18th September 2018.
Add Comment