Until few weeks back, I thought I understand when to make fields and methods static
or non-static
. For example, when a field (say object of another class) is unique for any number of objects for the class, it should be made static
.
But then I read about JVM garbage collection a few weeks back.
I understand that static
fields are never garbage collected and remain in memory all along, unless the class loader itself is garbage collected.
But if I don't make that field static
, at least it will be garbage collected.
So, it seems there is a very thin line between making fields/methods static or not.
Can anybody please explain to me this thin line in deciding, so that my application is way more efficient.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…