Kevik
Kevik

Reputation: 9351

static variables as global, and when are static objects garbage collected?

I want to access a variable anywhere in the android app, even if I never open to view the Activity class that the variable is declared in. Is a static variable possible to use for this. and if i move around between different activities can i access this variable to modify and change it without losing the changes?

i understand that shared preferences and sqlite database can be used to store variable for this purpose, however I wanted to know if I can safely use a static for this. my only worry is if for some reason while the app is still being used that the static variable will be garbage collected while the app is open and in use.

also i did not want to make a Application class global variable, instead i wanted to know about just using a static variable in a one of java or android class of the app.

Upvotes: 1

Views: 761

Answers (5)

Zulfiqar Chandio
Zulfiqar Chandio

Reputation: 263

Create a class AppConstants. then create your static variable as: public static String name="zz"; then access anywhere like Appconstants.name

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

Yes you can use static variable and no - it is not recommended as all your activities will have to have a reference for that class that holds that static variable (this means that your code will be tightly coupled!).

The "right" way to do it is either pass variable between activities or use sqlite - like you mentioned.

Upvotes: 0

g00dy
g00dy

Reputation: 6778

Quote from the thread here:

static fields are attached to the Class instance as a whole, which is in turn attached to the ClassLoader which loaded the class. the_instance would be unloaded when the entire ClassLoader is reclaimed. I am 90% sure this happens when Android destroys the app (not when it goes into the background, or pauses, but is completely shut down.)

Depending from your needs, try with a global variable first, but I think that the best method here is to use the SharedPreferences (just my opinion), but this is of course if it fits your model.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Static variables are not eligible for garbage collection till the containing class remains loaded in JVM. Such variables are referenced by Class objects which are referenced by ClassLoaders. Tehy can be garbage collected only in two very rare scenarios, if either the ClassLoader drops the Class somehow or the ClassLoader itself becomes eligible for collection.

More details from JLS § 12.7:

A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector as discussed in §12.6. Classes and interfaces loaded by the bootstrap loader may not be unloaded.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49362

Static variables cannot be eligible for garbage collection while the class is loaded. They can be collected when the respective class loader drops the class or is itself collected for garbage.

Upvotes: 1

Related Questions