Application子类中的静态变量。 (机器人)

时间:2013-11-14 15:52:27

标签: android

在Android应用程序的Application子类中有一个静态变量是否有任何意义。 如果我理解正确,那么应用程序是一个单例,它在应用程序启动时实例化,其公共变量将是全局变量。 如果我将变量声明为" public static"在Application子类中,我是否毫无意义地创建了一个全局变量全局变量?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

If I understand things correctly, the Application is a singleton that's instantiated when the app is started
是的,从操作系统的角度来看它可能是一个单例,但除非你实现它,否则不是你的 - 在你的类中创建一个静态引用并提供一个静态方法getInstance()

and its public variables will be global
是的,但只能直接访问静态变量。您将需要提供getInstance()方法以获取非静态实例,然后您才能访问类公共声明的变量。到目前为止,从Java和OOP的角度来看,没什么新东西。

If I declare a variable as "public static" in the Application subclass, am I just pointlessly making a variable global that would have been global anyway?
是的,你可以在其他任何地方定义这些。

要添加更多:你不应该太在意如何获得Application类的实例,因为它似乎是一种不好的做法。 Application有其自己的含义和逻辑,并且同时获得对它的引用是危险且毫无意义的。

似乎与another similar question.

相关
相关问题