java question

Started by fireside, December 06, 2008, 11:17:28 PM

Previous topic - Next topic

fireside

If I have a class with an array like:

class thing{
private static int[][] joe;

void doSomething{
joe = new int[20][20];
}
}

Is the array destroyed after doSomething() is called?  I would think it is but I'm not sure.  Also, would it be better to do that, or refill the array with 0's every time?
click here->Fireside 7 Games<-

EgonOlsen

No, it isn't destroyed. Why should it? It's a static member variable of that class. It will live as long as the class lives.

paulscode

If you mean is the original data removed from memory, I think it depends on if there are other varriables pointing to the original int[][].  If not, it will remain in memory until Java's garbage remover gets to it.  Either way, variable 'joe' will point to a different int[][] when the 'doSomething' method is called.

fireside

#3
OK thanks.  I guess I'll just try it that way and see how things work out.  I find it a little confusing with statics.  If it's pointing to a new place it's not really useful as a static anymore. I might be better off keeping it as a static variable in the class by allocating memory and flood filling with 0's instead, or it might not matter.  It would probably use less memory if I flood fill with zero's because garbage collection might not occur immediately.
click here->Fireside 7 Games<-

paulscode

#4
The purpose of static varriables is that you don't need to create an actual instance of a class to access them.  In your example class, varriable 'joe' would be the same for every instance of the class.  So for example if you did this:

thing thing1 = new thing();
thing thing2 = new thing();

In that case, thing1's 'joe' would point to the same int[][] array as thing2's joe, so altering the array in one instance would affect the other and vice versa.

Another useful thing about static varriables is that if they are public, then you don't need an actual instance of the class to access them.  For example, in your example class if you made 'joe' a public static int[][], then it could be accessed from anywhere in your program like this without ever creating a new 'thing' object:

thing.joe = new int[20][20];

Static varriables are really useful to use for global settings.  For example, in my SoundSystem library, the class 'SoundSystemConfig' uses static varriables in this way.  In that case, I made the static varriables themselves private, and made public static synchronized methods for accessing them.

Maybe a good way to think of static varriables is like there is just one instance of a class in the whole program.  If you think of it from that perspective, you can see they are not really that much different than regular varriables.  They still act as a handle to some set of data, and they follow all the same rules for Java varriables.  Only difference is there is only one copy of each static varriable, and it exists whether or not the class was instantiated in the program.