Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Developer
adisonjames
Posts: 44
Registered: 08-06-2009

workaround for Static instance of Ui/Application

[ Edited ]

Hello,

 

I have been recently reading more about RuntimeStore and Singleton and how it is "best practice" not to create a static instance of Ui/Application.

 

I have coded myself up a storm where I reference the Ui/Application in the

public static void main(String [] a){} so I need for the application instance to be static.

 

Because I have coded with this critical mistake (although I have managed to keep some complicated code bits together) I am thiking I should root out this problem before I go any further.

 

The main reason I use an instance of Ui/Application in the main method is due to the fact that I check for RuntimeStore instances and if my application instance does exist, I need a "container" Ui/Application static instance to receive the Singleton.

 

How would I go about uprooting my issue?  I will try to parse out some of my code later.

 

Thank you for your help!

Please use plain text.
Developer
peter_strange
Posts: 14,614
Registered: 07-14-2008

Re: workaround for Static instance of Ui/Application

I'm not sure I understand why RuntimeStore does not provide you with an easy answer to this question.

 

Currently I presume you do something like:

 

MyAPP....

 

to reference things that are 'static' in your application, i.e. shared stuff that you can access from anywhere.

 

I suspect that you can just replace all of these with

 

MyAPP.getInstance().....

 

Where getInstance obtains the singleton entry in RunTimeStore that has the data you want.  I think this KB article explains this:

 

How to - Create a singleton using the RuntimeStore
Article Number: DB-00686
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/832062/How_to_...

Please use plain text.
New Developer
adisonjames
Posts: 44
Registered: 08-06-2009

Re: workaround for Static instance of Ui/Application

[ Edited ]

I am all backed up and about to attempt some refactoring.  The reason that my application instance is static is because I refer to the instance in auto start modules while in the static main method.

 

This in turn is because, in my auto startup module I make a singleton of MyAppInstance and then refer again to this instance in the "launch" module entry point for the static main method.

 

Thank you for your guidance!

Please use plain text.