In C, scoping rules aside, the following are pretty much the same:
static int x[] = { /* ... */ };
int foo()
{
/* do something with x */
}
and
int bar()
{
static int x[] = { /* ... */ };
/* do something with x */
}
The static array x will go into the .data section of the executable. When the OS loads the executable into memory, it will ensure that the values for x are set in the appropriate place (e.g. by mapping its disk page to the virtual address in the ELF headers). In the second case, x is given some alias to avoid symbol conflicts.
There seems to be no way to do the second version in Java. Here’s how I tried:
static int xyzzy()
{
final int x[] = { /* ... */ };
/* do something with x */
}
This doesn’t do what you might think it does. While x cannot be changed, it is the reference to an array; the array elements themselves can be changed at will. Thus javac is unlikely to do anything smart here.
Indeed, in the above case, javac will initialize x with stack pushes every time xyzzy() is called. I had a real method like this, and making x a static member variable gave me an easy 2x speedup.