使用类在 Java 中存储静态数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12157991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:49:59  来源:igfitidea点击:

Using a class to store static data in Java?

javaandroid

提问by Arturs Vancans

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.

我目前正在为 android 开发一个应用程序,但这个问题对于 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.

在 android 的情况下,我正在跨活动移动,我想在那个单独的类中存储一些全局/静态标志/变量,并能够从我想要的任何活动中访问它们。

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

附注。仅会话时间需要数据。

采纳答案by Andro Selva

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.

嗯,这不是一个坏主意。您可以在 Android 中使用这种类型的类。但这里有一个小的修正。您可以使该类扩展 Application 类并使用它来存储数据,而不是维护一个包含静态数据的类。

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.

说到 SharedPreference,你应该只在需要长时间存储值时才考虑使用它们。如果没有,您应该使用 Application 类并使用 setter 和 getter,这是执行此操作的合法方法。

回答by Chirag

You can use SharedPreference to store flags and variables .

您可以使用 SharedPreference 来存储标志和变量。

Look at this Preference Demo.

看看这个首选项演示

回答by jeet

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

我建议您使用 Application 类,而不是创建静态类来保存全局变量。

see Link:

见链接:

Android global variable

Android 全局变量

回答by Mohammod Hossain

use SharedPreference to store flags and variables

使用 SharedPreference 来存储标志和变量

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

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

最好使用 Android Application 类。它旨在存储全局应用程序状态

回答by Alex Muni

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")

当您访问另一个活动时,您可以使用抛出一些值 Intent.putExtras(Name_parameter, "value_parameter")

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

并接收值:Bundle b = getIntent().getExtras();其中 b 具有所有参数

cheers,

干杯,

回答by nick.katsip

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

我建议您应该使用 Singleton 类封装全局类。在单例设计模式中查看更多信息

回答by Abhishek Bhandari

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)

好 !据我所知,这可能取决于您的项目规模!如果它相对较大,最好使用单独的类来存储静态数据(不仅仅是一个类,您可能需要根据所使用的静态数据的类型保留两个或多个类)