Static Variables, What Is Their Life Span?
Solution 1:
Static variable gets loaded when class is loaded by ClassLoader, and would be removed when it is Unloaded
Solution 2:
For the Next Readers of this question-
As Everybody said in the answer that static variables are class variables. They remain in the memory until the class is not unload from JVM.
In Android you have seen that when we close any application then it does not close completely, It remains in the recent application stack, That you can see by long press the home button(On Most Devices).
Android itself kicked out those recent apps when the other app needs memory
In Android, static variable unload when-
-You force stop your app.
-Application crashes.
-You clear your app data.
-Switch off your Device.
-Android kicked out recent app
Solution 3:
The static variable will live as long as the class is loaded in the JVM. When there are no more instances of the class being ran in the JVM the class will be unloaded and the static variable will be eligable for garbage collection.
Solution 4:
In addition to the other answers, also note that if those static "variables" are actually "static final" primitive constants, then they don't really exist as separate entities at all, but their value gets compiled right into all the classes that use them (not just the one that defines them).
Solution 5:
Static variables are associated with a class and they will live as long as the class is in the memory(which ceases to exist once your application terminates).
Post a Comment for "Static Variables, What Is Their Life Span?"