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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:52+00:00 2026-05-28T07:43:52+00:00

I have been having a hard time understanding how to use a singleton to

  • 0

I have been having a hard time understanding how to use a singleton to share a common variable. I am trying to make a blackberry app which has two entry points which need to share a common variable, iconCount. I have been advised to use a singleton with the RunTimeStore API by someone on a forum. Googling around eventually leads to:

http://docs.blackberry.com/en/developers/deliverables/17952/CS_creating_a_singleton_by_using_rutnime_store_1554335_11.jsp

I have been a few pages deep in Google but I still can`t understand what this does and how to implement it. My current understanding is that a singleton will create a “global variable” somehow through the code:

class MySingleton {
   private static MySingleton _instance;
   private static final long GUID = 0xab4dd61c5d004c18L;

   // constructor
   MySingleton() {}

   public static MySingleton getInstance() {
      if (_instance == null) {
         _instance = (MySingleton)RuntimeStore.getRuntimeStore().get(GUID);
      if (_instance == null) {
         MySingleton singleton = new MySingleton();

         RuntimeStore.getRuntimeStore().put(GUID, singleton);
         _instance = singleton;
         }
      }
      return _instance;
   }
} 

And another question would be how would I create a variable from this singleton? I need to declare variable iconCount = 0 at the beginning and then be able to use it. Would declaring it be something like

Integer iconCount = (Integer) RuntimeStore.getInstance(); 

? This is very new to me as I have just started Java so if anyone could explain this keeping in mind you’re communicating with a novice I would be very grateful. Thanks in advance!

  • 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-28T07:43:53+00:00Added an answer on May 28, 2026 at 7:43 am

    They mean please make sure that user initializing MySingleton class just onetime so you will not have problem with multiple instances and initialize two count in the same time. I mean from multiple instance something like below:

    Mysingleton single = new Mysingleton();
    Mysingleton single2 = new Mysingleton();
    

    Because both initilaization can have diffetent count. You need something like this:

    public class IconManager {
        private static iconManager _instance;
        private static final long GUID = 0xab4dd61c5d004c18L;
        private static int count = 0;
    
        // constructor
        IconManager() {
        }
    
        public static IconManager getInstance() {
            if (_instance == null) {
                _instance = (IconManager) RuntimeStore.getRuntimeStore().get(GUID);
                if (_instance == null) {
                    IconManager singleton = new IconManager();
    
                    RuntimeStore.getRuntimeStore().put(GUID, singleton);
                    _instance = singleton;
                }
            }
            return _instance;
        }
    
        public static int getCount() {
            return count;
        }
    
        public static void setCount(int count) {
            this.count = count;
        }
    

    }

    and after you can create an instance for the class:

    public static void main(String[] args){
    
        IconManager iconManager = IconManager.getInstance();
        iconManager.setCount(iconmanager.getCount() + 1);
    
    }
    

    So application will do first validation, if there is already an instance it will update existing one, if not than it will create new one.

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

Sidebar

Related Questions

So I have been having a hard time sending email from my school's email
I'm new to iPhone development, and been having a hard time trying to figure
I have this code I been working on but I'm having a hard time
I have been having a really hard time debugging this error since the past
i have been having a hard time to find anything that is usefull but
Ok, I have been having a VERY hard time figuring this out. I guess
This is probably a basic question, but I have been having a hard time
I've been reading about Haskell and I'm having a hard time understanding how function
I have been having some problems trying to get my PHP running. When I
I've been having a hard time coming up a solution with this one. I'm

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.