静态变量:好还是坏? [英] Static Variables : Good or Bad?

查看:116
本文介绍了静态变量:好还是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么静态变量被认为是邪恶的?

I习惯在我的所有程序中广泛使用静态变量,特别是在我使用Android时。我倾向于使用它们,因为有时通过Intents 发送10个或更多值会感觉非常麻烦。所以,我只是将它们声明为静态变量,并使用点运算符轻松地在其他类中访问它们。使用静态变量的另一个原因是我在整个应用程序中使用实用程序类。就像我在下面给出的代码帮助我在不同的活动中使用变量

I have the habit of using static variables extensively in all my programs, especially when I am working with Android. I tend to use them because sometimes it feels so cumbersome to send 10 or more values via Intents. So, I just declare them as static variables and access them in the other classes easily by using the "dot" operator. Another reason for using static variables is when I am making a Utility class to be used throughout my application. Like the code I have given below helps me to use the variables in different activities.

Utility.java

public class Utility {
public static Facebook fb;
public static AsyncFacebookRunner fbAsyncRunner;
public static String[] fbPermissions = {"email", "read_stream", "user_birthday"};
public static final String PREF_UTILITY_FILE_NAME = "PrefUtilityFile";
public static SharedPreferences prefs;
public static Editor editor;
public static String access_token;
public static long expires;
}

我在网上搜索了类似的问题并遇到了这个这个,但它们似乎没有给出问题的最终答案。在大多数地方,我看到相互矛盾的意见,因此我感到非常困惑。

I searched online for similar questions and came across this and this, but they do not seem to give a final answer to the issue. And in most of the places, I see conflicting opinions and hence am totally confused.

这是一个好的编程习惯还是坏的?我应该是否使用它?

推荐答案

您可以用Context对象替换所有静态字段,您可以传递或做一个单身人士。可以删除几乎所有的静态字段。这是否是一个好主意取决于你,但我不认为使用实例字段要困难得多。

You can replace all you static fields with a "Context" object which you can pass around or make a Singleton. It is possible to remove almost all your static fields away. Whether this is a good idea or not is up to you, but I wouldn't assume that it has to be much harder to use instance fields.

BTW:我会建议


  • 将静态字段/常量与使用它们的类或包放在一起

  • 处理静态数组如果可能的话,使它们成为最终

  • placing static fields/constant with the class or package which uses them
  • treat static arrays as immutable if possible making them final as well.

您可以使用非静态上下文

You can use a non-static Context with

public class Context {
    public static final String PREF_UTILITY_FILE_NAME = "PrefUtilityFile";

    public Facebook fb;
    public AsyncFacebookRunner fbAsyncRunner;
    public String[] fbPermissions = {"email", "read_stream", "user_birthday"};
    public SharedPreferences prefs;
    public Editor editor;
    public String access_token;
    public long expires;
}

// pass to constructor as required
class UsesContext {
    final Context context;
    public UsesContext(Context context) {
        this.context = context;
    }

    public void method() {
        // can use context
    }
}

这允许您创建具有多个上下文的单元测试。

This allows you to create unit tests with multiple Contexts.

我唯一会留下静态是常量。

The only thing I would leave static are constants.

这篇关于静态变量:好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆