1

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.

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the speedy reply @gunar. However I still don't fully get it. Here's my question in another way. If I extend application with "MyApp extends Application", and add a "public String myString" to it, then I can access myString globally and since there's only one copy of MyApp there is only one copy of MyString ever created. From what I can see a static variable is also accessible globally and there's only one copy of it.
Here's the full version...Thanks for the speedy reply @gunar. However I still don't fully get it. Here's my question in another way. If I extend Application with "MyApp" and add a "public String myString" to it, then I can access myString globally and since there's only one copy of MyApp there is only one copy of MyString ever created. If I create a static myString it's also accessible globally and there's only one copy of it. Is there any reason for using a static myString inside MyApp when all of it's public methods already seem to do the same thing (performance, readability, etc.)??Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.