Are there any issues with importing static variables & arrays ?

Hi,

Occasionally i read that people aren’t that keen on using static variables. Untill recently i didn’t really understood why this could be potentially problematic until some of my Texture images got just disappeared when i tried running my game for a second time on my android device: -> http://www.java-gaming.org/topics/android-libgdx-images-disappear-after-i-start-my-debug-apk-from-widget/34441/view.html

This tought me that i should not load my images using static fields.

But what about variables and arrays.
I still find it very convenient of storing variables or arrays I am using over different classes in one class and then be able to access those variables or arrays by just importing the class i created for the variable and/or arrays with a simple import statement; f.e.

import static "packageName".Variables.*

Now everytime i need to use those variables/arrays it’s not necessary to reference to the specific class every time.

But I was wondering if there are any issues with this just like when I was using static fields for loading my Images??

Are you talking about phone app or computer application? I don’t think static variables can cause problems after exiting in computer applications.

EDIT------

This happens, because you don’t dereference static images. When you exit your android app, the opengl context might delete your textures, but you will still keep pointers to them in static field. This happens, because android doesn’t free up your apps memory, unlessit needs to. Or something like that.

Yeah, the question is about phone/Android devices.

On my computer everything runs fine, but i was wondering if using static fields for variables & arrays would cause me problems if i want to run my game on mobile devices in the future

The short answer is that yes, abusing (or misusing) the static keyword can definitely lead to huge headaches.

The static keyword is used for a specific purpose: when you have data that isn’t associated with a particular instance. Think constants, or shared data, or utility functions.

Misusing it to make it “easier” to access the data of a particular instance is a horrible idea. It’s just as bad as making everything public for the same reason.

A real-life example: at my job, we have a piece of map software. This map software was written using static variables all over the place, just to make it easier to pass information around- after all, the program only ever had a single map, so we only really had one instance to deal with anyway.

That worked fine, until a requirement came down to show two maps next to each other. That job turned out to be much harder than it should have been, because of the misuse of the static keyword. We had to go in and refactor all of the code, when it should have been as simple as just creating a second instance.

So misusing the static keyword is a very very very bad habit to get into. It’s a pretty horrible code smell, and tells me that you should either refactor your code or get into better coding habits.