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 6853801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:33:17+00:00 2026-05-27T01:33:17+00:00

There are many questions and answers on how to implement a global variable in

  • 0

There are many questions and answers on how to implement a global variable in Android/Java.

So it seems one can either implement a singleton or use a data class itself with static variables.

I am about to start a larger project and would like to start on the right foot.

I am just not sure which one to use.

Pro singleton/con Data Class

  • supposedly “cleaner” way (but I really don’t know why)
  • ensures that there is really always just one representation
  • creates a new instance should the old one be “cleaned away” (whenever this may happen?)

Con singleton/pro Data Class

  • not recommendet by some (but did not find convincng reasons)
  • ensures that there is only one representation by design
  • very easy to access just by writing MyDataClass.x (vs accessing singleton requires getting access to it first somehow)
  • no need to pass it as a parameter

So in summary I tend to use DataClass but I am unsure because I read that this is supposedly not good programming style.

I like to add

  • the data this global object has to hold is quite big, more than 30k strings/keys. And this should not be cleaned at any stage so that when the app return it may crash because of that – as I read in other places eg Singletons vs. Application Context in Android? (the 3rd answer)
  • it’s not a web application, I use only one classloader
  • it is multithread but only one thread is actually accessing this data
  • one may certainly also use this approach How to declare global variables in Android?, but isn’t an ObjectClass just easier to use and access in this case?
  • And checking this http://developer.android.com/resources/faq/framework.html, esp under “Persistent Objects”, implies that there is no real advantage for on or the other in those cases anyway.

Many thanks

  • 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-27T01:33:18+00:00Added an answer on May 27, 2026 at 1:33 am

    Best way to implement singleton is to use enum.

    public enum Singleton
    {
        INSTANCE;
    
        public void someMethod()
        {
            // your code here
        }
    }
    

    For more details you can read Effective Java (2nd Edition)

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

Sidebar

Related Questions

First of all: I do know that there are already many questions and answers
I know there are already many questions like mine but I found no answer
There are many questions discussing the details of C and C++ dealing with pointer-to-const
NOTE: I know there are many questions that talked about that but I'm still
First, I noticed there are many questions regarding this, lots marked as duplicate. I
There are many similar questions, but apparently no perfect match, that's why I'm asking.
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
There seem to be many questions asked about this subject here on stackoverflow, but
There are many skills a programmer could have (understanding the problem, asking good questions,
I know that there are many Delphi database related questions available, but I'm considering

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.