8

Is it a bad idea creating a separate class and use it as a storage which consists only of static data variables?

I am currently developing an app for android, but the question is general for Java.

In case of android, I am moving across activities and I would like to store some global/static flags/varibles in that separate class and being able to access them from any activity I want.

PS. The data is required only for the session time.

1
  • For the lifetime of your app it is a good decision. for longlife storage use the sharedpreferences or sqlite. Commented Aug 28, 2012 at 11:12

7 Answers 7

7

Well, that's not a bad idea. You can use such type of a class in Android. But a small correction here. Instead of maintaining a class that holds static Data, you can make that class to extend Application class and use it store the data.

Here is a example,

public class HelloApplication extends Application {
        private int globalVariable=1;

        public int getGlobalVariable() {
                return globalVariable;
        }

        public void setGlobalVariable(int globalVariable) {
                this.globalVariable = globalVariable;
        }
        @Override
        public void onCreate() {
                //reinitialize variable
        }
}

And in your Activity, do this,

(HelloApplication)getApplication()).setGlobalVariable(10);
int valiable=((HelloApplication)getApplication()).getGlobalVariable();

Taken from here..

And speak about SharedPreference, you should consider using them only when the value has to be stored for a long time. if not, you should make use of the Application class and use setters and getters which is the legitimate way to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

I think one problem is you have to do a cast everytime you want to access or modify a variable. Using a static class with statics methods/variables saves multiples casts and so process and memory, doesn't it ?
It is not necessary to cast every time if we store the casted object alone and make use of it through out..
It is a good idea, but I believe creating setters and getters each time I add a new variable is troublesome. However, I must have a look on advantages using Application extension.
5

You can use SharedPreference to store flags and variables .

Look at this Preference Demo .

1 Comment

I need it only for a session time and I want the setting to reset on every new app launch.
2

Instead of Creating a Static class to save Global Variables, I would suggest you to use Application class.

see Link:

Android global variable

Comments

1

use SharedPreference to store flags and variables

http://developer.android.com/reference/android/content/SharedPreferences.html

For Global variable:

it would be better to use the Android Application class. It's meant to store global application state

Comments

1

i suggest that "never" use a global variables ....

when you are accessing to another activity you can throw some values using Intent.putExtras(Name_parameter, "value_parameter")

and to recive the value : Bundle b = getIntent().getExtras(); where b has all parameters

cheers,

2 Comments

The question is not about passing data from an activity to another, it's about accessing data whatever its nature from anywhere in the code.
Moreover, I meant generally in Java mentioning Android just as an example.
0

Well ! As far as I know It might depend on the size of your project ! If its relatively large its good to use separate class to store static data (not only one class , you might come in need of keeping them two or more classes depending on the type of the static data used)

Comments

0

I would suggest that you should encapsulate your global class with a Singleton class. See more at Singleton Design Pattern

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.