As per the concept about static members, they are created/loaded into the memory when there is first call made to its class. And they are common among all instances of that class. Means they are not re-created or re-itialized etc. In addition, They can be accessed by the class name only. There is no need to create object for that class just to access them.
Now my questions are;
- Whether static members ever be in
memory till the application is
running? even if all the instances
of that class had been collected by
GC(garbage collector). - For a big project, where 8-10 teams
are working together, they dont care
about the coding of other’s team.
They may create static members as
per their need. If all the members
are cached in memory, would it not
create overhead over JVM? - By default, all the members of interfaces are STATIC and the use of interfaces are good in many cases. But if i keep my above questions in mind, should i still use interfaces?
1) Static members are garbage collected only when the class that defines them is itself collected; this in turn can only happen if the defining ClassLoader is collected. This is common in web application containers and plugin architectures.
2) Yes, defining a large amount of static data can be a bad idea. But it’s like a lot of other things: it’s good if you need it, and bad if you abuse it. Just use common sense.
3) Again, an interface that defined an array of a thousand Strings would be a bad idea, but of course that’s not normally what people do. Just use common sense. There’s no (memory-related) reason to avoid static variables in general.