ShanieMoonlight
ShanieMoonlight

Reputation: 1861

Static variable in Application subclass. (Android)

Is there any point in having a static variable in a subclass of Application in an android app. If I understand things correctly, the Application is a singleton that's instantiated when the app is started and its public variables will be global. 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?

Thanks for your help.

Upvotes: 1

Views: 304

Answers (1)

gunar
gunar

Reputation: 14710

If I understand things correctly, the Application is a singleton that's instantiated when the app is started
Yes, it is a singleton from OS point of view maybe, but not from yours unless you implement it - make a static reference in your class and provide a static method getInstance().

and its public variables will be global
True, but only only static variables will be accessed directly. You will need to provide the getInstance() method in order to get the non-static instance and only then you'll have access to class public declared variables. So far nothing new from Java and OOP point of view.

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?
Yes, you can very well have these defined anywhere else.

To add more: you shouldn't care that much about getting an instance of the Application class anyway as it seems a bad practice. An Application has its own meaning and logic and getting a reference to it is dangerous and pointless at the same time.

May seem related to another similar question.

Upvotes: 1

Related Questions