Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6321387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:08:24+00:00 2026-05-24T16:08:24+00:00

I am using the android.app.Application class (a subclass) to store some global information. An

  • 0

I am using the android.app.Application class (a subclass) to store some “global” information. An example is the user location the last time we grabbed it from the GPS/wifi. My question is whether I should be storing these “globals” as static variables or instance variables. Not sure which scenario is better or more correct.

Scenario A: using static variables —

public class MyApplication extends android.app.Application {

    private static Location myLocation;
    public static Location getLocation() {
      return myLocation;
    }
    public static void setLocation(Location loc) {
      myLocation = loc;
    }
}

Scenario A: usage —

loc = MyApplication.getLocation();
MyApplication.setLocation(loc);

Scenario B: using instance variables —

public class MyApplication extends Application {

    private Location myLocation;
    public Location getLocation() {
      return this.myLocation;
    }
    public void setLocation(Location loc) {
      this.myLocation = loc;
    }
}

Scenario B: usage —

loc = getApplication().getLocation();
getApplication().setLocation(loc);

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:08:25+00:00Added an answer on May 24, 2026 at 4:08 pm

    Using Static variable is the right way always, since its the single state you want to maintain everywhere. But why taking risk when you have something like SharedPreference() provided by java, which is more secure and you can always be sure about it. and get it everywhere across your your application. usually maintaining states of variable is a risky task.

    problems of using instance variable to maintain state

    Instance variables are different for different objects/Instance you create (So they are called instance variable) so if try to get a value of the variable by creating a new object, it returns something you don’t want (since it was set in another instance), where as if you use static variable every instance/object refers the same variable (i mean to say same memory location) so what ever changes are made by any instance is reflected through out the application, does’t matter how you access it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which extends android.app.Application , which I use to persist global
I'm having some errors with my custom recording application using android. I saw most
I need to implement some functions into an Android application using NDK and thus
Using C#, I need a class called User that has a username, password, active
I am developing an application in Eclipse build ID 20090920-1017 using android SDK 2.2
I'm starting an Android app which will have multiple Activities. It will be using
I have developed some reusable android component which is basically a class . This
I am working on an Android app that has multiple screens the user will
I'm trying to debug an android app that call native code to do some
I am still working on my location-based alarm android application. I have an AlarmService

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.