Re: about static variable
- From: "andreas kinell" <news@xxxxxxxxx>
- Date: Mon, 31 Oct 2005 17:55:20 +0100
"George2 via JavaKB.com" <u14844@uwe> schrieb im Newsbeitrag
news:56a3fd504a8ab@xxxxxx
> Hello everyone,
>
>
> I have heard that static variable will make the program consume very much
> memory. It is because garbage collector (GC) will not work on static
> variable.
> Is that correct?
A static variable is a class variable and therefore will reside in memory
even when no object of that class type exists. It does not consume very much
memory unless you load a lot of classes with big static arrays and don't
instantiate them.
If you have an instance (an object), it doesn't really matter if the
variable is static or not, it will be on the heap anyway. If you have a lot
of instances of the same class type, then you save memory by declaring the
variable static (you will have only one, instead of one for every instance).
BUT: the decision static or not should be made considering OO-Design choices
rather than memory problems.
> I am also wondering when does the memory space of a static variable is
> allocated, during compile or during runtime?
Of course the memory is allocated when starting the program. Compiling has
nothing to do with program execution.
andreas
.
- References:
- about static variable
- From: George2 via JavaKB.com
- about static variable
- Prev by Date: How to compile a java program w/ another java programs which are w/o a main method
- Next by Date: Re: Crazy Clock
- Previous by thread: Re: about static variable
- Next by thread: variables?
- Index(es):
Relevant Pages
|