Thursday, February 7, 2013

Actual location of static variables in Java


Static variable is allocated for the entire duration of program's execution, so neither stack nor heap are convenient for it. Static fields are initialized when a class is loaded and and are discarded when the class loader for that class is unloaded.
The memory is allocated at the start of the program, in regular memory, instead of the stack . The advantage of this is that it makes your variable or procedure totally constant, and you can't accidentally change the value. The disadvantage of this is that the memory is not deallocated until the program is terminated.

No comments:

Post a Comment