初始化静态最终变量在声明VS Application.OnCreate期间初始化() [英] Initializing static final variable in declaration vs initializing during Application.OnCreate()

查看:474
本文介绍了初始化静态最终变量在声明VS Application.OnCreate期间初始化()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储的活动(已经考虑Parcelables并,JSON,脚本非常重要)之间的一个对象的状态,但因为我有几个单身的,还不如引用它们在扩展应用程序的类(模块+易维护)

所以,我的问题,比方说我有一个简单单:

 类SimpleSingleton
{
    私有静态最终SimpleSingleton实例; //这个问题稍后将参照这一行。
    公共静态SimpleSingleton的getInstance()
    {
        返回实例;
    }    私人SimpleSingleton(){}
}

1:起初我上面的类中创建的InitInstance()方法,例如:

 类SimpleSingleton
{
    // ...在code以上    公共静态无效的Ini​​tInstance()
    {
        如果(例如== NULL)实例=新SimpleSingleton();
    }
}

2:因此,下面的作品,(在此之后,我可以通过指从任何活动的单身CustomSingleton.getInstance()

 类MyApp的扩展应用
{
    @覆盖
    公共无效的onCreate()
    {
        super.onCreate();
        initSingletons();
    }    保护无效initSingletons()
    {
        SimpleSingleton.initInstance();
    }
}

但。如果我声明一下

私有静态最终SimpleSingleton实例=新SimpleSingleton();

而不是

私有静态最终SimpleSingleton实例;

在SimpleSingleton类?

我想在编译时初始化对象,因此不会使整个#1和#2不必要的?或者我拿到订单错误的(尤其是上课的时候​​实际上是初始化)?我从C#来了,目前正在开发Android所以这有点给了我一个快速的疑难杂症时,我想引用我单身。另外,我问这个<一个href=\"http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/\"相对=nofollow>根据此博客因为:


  

怪异的行为,我看到更有意义,我的解释是,静态变量实例绑定到第一次初始化它们的类的类加载器。



解决方案

我能想到的唯一的区别是,当你做

 私有静态最终CustomObject实例=新CustomObject();

当你启动应用程序时,将创建并为它分配空间。
注意它可能永远不会被使用,但它仍然是使用内存。

当您在onCreate方法只会创建时,它被称为实例创建它。

使用静态也有一个更缺点是它会使用你的烫发根空间,如果碰巧它不能给它的空间或未能创建你的程序在启动时崩溃。让你感到困惑。

我强烈建议您使用onCreate方法的方法。

I want to store an object state between activities (already considered Parcelables, JSON, yadda yadda) but since I have a couple of Singletons, might as well refer to them in a class that extend Application (modularity + easy to maintain).

So to my question, let's say I have a simple singleton:

class SimpleSingleton
{
    private static final SimpleSingleton instance;   //The question will refer this line later.
    public static SimpleSingleton getInstance()
    {
        return instance;
    }

    private SimpleSingleton(){}
}

1: At first I create an initInstance() method within the above class, e.g:

class SimpleSingleton
{
    //... the code above

    public static void initInstance()
    {
        if(instance == null) instance = new SimpleSingleton();
    }
}

2: Hence the below works, (in which afterwards, I can refer to the singleton from any activity via CustomSingleton.getInstance()):

class MyApp extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        initSingletons();
    }

    protected void initSingletons()
    {
        SimpleSingleton.initInstance();
    }
}

BUT. What if I declare

private static final SimpleSingleton instance = new SimpleSingleton();

instead of

private static final SimpleSingleton instance;

in the SimpleSingleton class?

I assume the object is initialized during compile time, so doesn't that makes the whole #1 and #2 unnecessary? Or do I get the order wrong (especially WHEN the class is actually initialized)? I came from C# and currently developing for Android so this kinda gave me a quick gotcha when I want to refer to my Singletons. Also, I ask this since according to this blog:

The explanation of the weird behavior I saw that makes more sense to me is that the static variables instances are bound to the class loader of the class that first initialized them.

解决方案

The only difference i can think of is when you do

private static final CustomObject instance = new CustomObject();

when you application is launched it will create and allocate space for it. Note it might never be used but it would still be using memory.

when you create it on an onCreate method it will only create an instance when it is called.

Using static also has one more disadvantage that is it will use your perm gen space and if by chance it fails to give it space or fails to create it your program will crash on startup. Leaving you confused.

I strongly suggest using the onCreate method approach.

这篇关于初始化静态最终变量在声明VS Application.OnCreate期间初始化()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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