Author Topic: java question  (Read 1857 times)

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
java question
« on: December 06, 2008, 11:17:28 pm »
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?
« Last Edit: December 06, 2008, 11:24:23 pm by fireside »
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: java question
« Reply #1 on: December 06, 2008, 11:30:07 pm »
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.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: java question
« Reply #2 on: December 06, 2008, 11:54:21 pm »
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.

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
Re: java question
« Reply #3 on: December 07, 2008, 01:47:19 am »
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.
« Last Edit: December 07, 2008, 02:04:10 am by fireside »
click here->Fireside 7 Games<-

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: java question
« Reply #4 on: December 08, 2008, 05:29:36 am »
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:
Code: [Select]
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:

Code: [Select]
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.
« Last Edit: December 08, 2008, 05:42:17 am by paulscode »