应用程序子类中的静态变量.(安卓)



Android应用程序的子类中具有静态变量有什么意义吗?如果我理解正确,应用程序是一个单例,在应用程序启动时实例化,其公共变量将是全局的。如果我在应用程序子类中将一个变量声明为"公共静态",我是否只是毫无意义地使变量全局化,无论如何都是全局的?

感谢您的帮助。

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为 True,但只有静态变量会被直接访问。您需要提供 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有其自身的含义和逻辑,同时引用它是危险且毫无意义的。

似乎与另一个类似的问题有关。

最新更新