Global Variables

Showing 1-46 of 46 messages
Global Variables David Williams 3/9/11 12:41 PM
All,

What is the best way of going about setting up global variables?  There will be like 5-6 global variables that I would like to set when my app is launched that are then available for any code anywhere in my app.
I did something similar to this under Mojo on WebOS.  I just set some global variables during the stage-assistant.js script that could then be used by any script.

TIA.
--

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.


Re: [android-developers] Global Variables TreKing 3/9/11 12:53 PM
On Wed, Mar 9, 2011 at 2:41 PM, David Williams <dwil...@dtw-consulting.com> wrote:
What is the best way of going about setting up global variables?

IDK about "best", but "easy": public static values somewhere that you set up in a custom Application class.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices

Re: [android-developers] Global Variables MagouyaWare 3/9/11 1:05 PM
The best way is to avoid them like the plague...

If you can't then you can either subclass Application and make them available there or you can use SharedPreferences...

Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Wed, Mar 9, 2011 at 1:41 PM, David Williams <dwil...@dtw-consulting.com> wrote:
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Global Variables David Williams 3/9/11 1:15 PM
Justin,

Thanks for the information, I will have to look into just what subclassing the Application means/is and also SharedPreferences.

That said, why avoid them like the plague?  If you don't use global variables then just how do you get some kind of static value (which is really what I'm after) available to all aspects of your app code?

TreKing, will look into the public static values as this might be more of what I need.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



Re: [android-developers] Global Variables MagouyaWare 3/9/11 1:16 PM
Or you can use public static like TreKing suggested... 

Personally, if I find that I'm having to use Global variables, then that means I'm most likely doing something wrong, and I try to restructure what I'm doing.  Generally I can find a way to accomplish the same thing without them and have a better application design.



Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


Re: [android-developers] Global Variables Chris Stewart 3/9/11 1:24 PM
I tend to do what TreKing suggested.  I have a class called CommonVariables that really holds static strings for Flurry event names, the year parameter for my app (it's seasonal and changes once a year) which is used all over the place, and things like that.

--
Chris Stewart





--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: Global Variables Kenny Riddile 3/9/11 1:13 PM
On 3/9/2011 3:41 PM, David Williams wrote:
> All,
>
> What is the best way of going about setting up global variables? There
> will be like 5-6 global variables that I would like to set when my app
> is launched that are then available for any code anywhere in my app.
> I did something similar to this under Mojo on WebOS. I just set some
> global variables during the stage-assistant.js script that could then be
> used by any script.
>
> TIA.
> --
> ------------------------------------------------------------------------

>
> David Williams
> Check out our WebOS mobile phone app for the Palm Pre and Pixi:
> <http://www.dtw-consulting.com/GolfCaddie> Golf Caddie
> <http://www.dtw-consulting.com/GolfCaddie> | Golf Caddie Forum
> <http://www.dtw-consulting.com/GolfCaddie/forum> | Golf Caddie FAQ
> <http://www.dtw-consulting.com/GolfCaddie/faq.html> by DTW-Consulting, Inc.

>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

IMHO, the best way to use global variables is to not use global variables.

Re: [android-developers] Global Variables MagouyaWare 3/9/11 1:25 PM
> why avoid them like the plague?

Here is some light reading on the subject:

http://c2.com/cgi/wiki?GlobalVariablesAreBad
http://en.wikipedia.org/wiki/Global_variable
http://stackoverflow.com/questions/2157685/why-no-global-variables-in-java-like-c

In general, it is considered a bad coding practice...


Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


Re: [android-developers] Global Variables TreKing 3/9/11 1:26 PM
On Wed, Mar 9, 2011 at 3:15 PM, David Williams <dwil...@dtw-consulting.com> wrote:
That said, why avoid them like the plague?

Global variables are one of those things, like Singletons, that on the surface seem to make life easier, then get abused like a step-child to the point of making everything worse.

Used correctly, in moderation, it's often the fastest, easiest, cleanest, and most straightforward way of doing something. Like a simple flag indicating DEBUG vs RELEASE, for example.
Re: [android-developers] Global Variables Kostya Vasilyev 3/9/11 2:01 PM
Oh, come on, guys, it's not that bad :)

Sometimes I even miss a good old-fashioned "goto".

-- Kostya

10.03.2011 0:26, TreKing пишет:
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


-- 
Kostya Vasilyev -- http://kmansoft.wordpress.com
Re: [android-developers] Global Variables David Williams 3/9/11 2:07 PM
ROFL.

10  PRINT "GLOBALS ARE FINE"
20  GOTO 10


Ah yes, the good old days :)

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



Re: Global Variables Kenny Riddile 3/9/11 2:10 PM
On 3/9/2011 4:26 PM, TreKing wrote:
> On Wed, Mar 9, 2011 at 3:15 PM, David Williams
> <dwil...@dtw-consulting.com <mailto:dwil...@dtw-consulting.com>> wrote:
>
>     That said, why avoid them like the plague?
>
>
> Global variables are one of those things, like Singletons, that on the
> surface seem to make life easier, then get abused like a step-child to
> the point of making everything worse.
>
> Used correctly, in moderation, it's often the fastest, easiest,
> cleanest, and most straightforward way of doing something. Like a simple
> flag indicating DEBUG vs RELEASE, for example.
>
> -------------------------------------------------------------------------------------------------
> TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago

> transit tracking app for Android-powered devices
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

Singletons are global variables.

Re: [android-developers] Re: Global Variables TreKing 3/9/11 2:13 PM
On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile <kfri...@gmail.com> wrote:
Singletons are global variables.

Uh ... no ... no they're not.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices

Re: Global Variables Kenny Riddile 3/9/11 2:30 PM
On 3/9/2011 5:13 PM, TreKing wrote:
> On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile <kfri...@gmail.com
> <mailto:kfri...@gmail.com>> wrote:
>
>     Singletons are global variables.
>
>
> Uh ... no ... no they're not.
>
> -------------------------------------------------------------------------------------------------
> TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago

> transit tracking app for Android-powered devices
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

Assuming the singleton is modifiable via its interface, then for all
intents and purposes, yes, they are.

Re: [android-developers] Re: Global Variables Scott Davies 3/9/11 2:43 PM

I think the idea is that because the singleton is encapsulated, you at
least have some business logic around mutating them, whereas the "classic"
global variable is visible AND mutable without restrictions.

- Scott


Re: [android-developers] Re: Global Variables Kostya Vasilyev 3/9/11 3:01 PM

What if the Singleton is a Facade or a Mediator? :)

10.03.2011 1:44 пользователь "Scott Davies" <code...@hotmail.com> написал:
Re: [android-developers] Global Variables David Williams 3/9/11 4:11 PM
Ok, trying to do this but struggling.

I created a class as follows:

package com.dtwconsulting.golfcaddie;

import android.app.Application;

public class globalVars extends Application{
   
    public String apiKey = "XXXXXXXXXXXX";
   
    public String getApiKey() {
        return apiKey;
     }
}

Now, how do I get the value of the apiKey from somewhere else?  Sorry, this is just my lack of knowledge on Java here.  I was hoping it was something like globalVars.getApiKey(), but that doesn't seem to work.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



Re: [android-developers] Global Variables Miguel Morales 3/9/11 4:16 PM
If you define to use your custom application class in the android manifest file you can get the class from your activities.

Inside your activity, simple use (MyAppliction) getAppliction() and then you can access your variables.
If it's static global variables you're looking for.  Then you would use MyApplication.MyStaticConst~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/ http://www.youtube.com/user/revoltingx
Re: [android-developers] Global Variables David Williams 3/9/11 4:24 PM
Sorry, I am new to Java and Android.

I don't get what you are saying.  I added the following android:name="globalVars" to the manifest.
But I still don't understand how I can get the value of the apiKey, which should be
XXXXXXXXXXXX.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



Re: [android-developers] Global Variables Miguel Morales 3/9/11 4:31 PM
Ah, sorry for my brief explanation.
Basically, you have to decide what you want.  
You can either encapsulate your application using Application sub-class which all your activities can then access.
Or, you can have a java class in which you store your global variables IF they don't change at runtime.

So, if you want the first option.  Say you have an Application subclass named MyApp.  You would add it to the manifest file like android:name="MyApp"
Android will use that class which you can then access from your activities using Activity.getApplication() method.
So you can cast that to MyApp like so:

onCreate {
    MyApp app = (MyApp) getApplication();
    MyVar var = app.getVar();
}

If you want the second option, you'd create a class called Consts.java in which you store static global variables.
So something like:
class Consts {
   public final static String AppName = "SOMETHING";
}

Then from your activity you can access it like:

String appname = Consts.AppName

Also, you might want to change your font, it's a bit hard to read your messages.
Re: [android-developers] Global Variables MagouyaWare 3/9/11 4:31 PM
Like this:

globalVars myApp = (globalVars)getApplication();
myApp.getApiKey();

Or... since you made the variable public you can do this:
myApp.apiKey;



Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


Re: Global Variables fadden 3/9/11 4:35 PM
On Mar 9, 2:13 pm, TreKing <treking...@gmail.com> wrote:
> On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile <kfridd...@gmail.com> wrote:
> > Singletons are global variables.
>
> Uh ... no ... no they're not.

I think this about covers it:

  http://sites.google.com/site/steveyegge2/singleton-considered-stupid
Re: [android-developers] Re: Global Variables TreKing 3/9/11 4:35 PM
On Wed, Mar 9, 2011 at 4:30 PM, Kenny Riddile <kfri...@gmail.com> wrote:
Assuming the singleton is modifiable via its interface, then for all intents and purposes, yes, they are.

Again, no.

Singleton is a design pattern whose purpose is to simplify and control access to a an object for which there is and will only be one instance of. This instance is set once and used throughout. It does not vary or change. It is not variable.

Of course you can change the *state* of the singleton object, if that's what you mean by "modifiable", but if you change the *value* of the object itself, as one would do with global *variables*, it is no longer a singleton.

Even in the sense that the singleton state is modifiable, the whole point of the singleton is to provide an interface through which this globally accessible object, and its state, is manipulated. There is controlled access. This is not the case with your run-of-mill, freely accessible global variable.

On Wed, Mar 9, 2011 at 6:11 PM, David Williams <dwil...@dtw-consulting.com> wrote:
Ok, trying to do this but struggling.

I created a class as follows:

If all you're doing is defining some *constants* that are not going to change, extending Application is way overkill. Especially since casting up to your Application type every time you need something makes your code horrendous to look at.

You can just do this:

class Constants
{
 public static final String KEY = "MyKey";
}

Then do Constants.Key where you need it. Done. 

On Wed, Mar 9, 2011 at 6:11 PM, David Williams <dwil...@dtw-consulting.com> wrote:
Sorry, this is just my lack of knowledge on Java here.  I was hoping it was something like globalVars.getApiKey(), but that doesn't seem to work.

I highly recommend you brush up on Java, reviewing static and instance level access of functions and data in particular. 
 
-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices

Re: [android-developers] Re: Global Variables MagouyaWare 3/9/11 5:02 PM
I agree with TreKing.


Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Global Variables David Williams 3/9/11 5:17 PM
Miguel (and others),

Many thanks for helping me understand this better.  I think the second option is the best for me so will try that.

As to my font, I don't know what to so.  I'm using Arial and it looks fine on my screen and also from my Android device.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



Re: [android-developers] Global Variables Miguel Morales 3/9/11 5:19 PM
No problem, looks like your font is showing up correctly now.  
Good luck on your app.

On Wed, Mar 9, 2011 at 5:17 PM, David Williams <dwil...@dtw-consulting.com> wrote:
Miguel (and others),

Many thanks for helping me understand this better.  I think the second option is the best for me so will try that.

As to my font, I don't know what to so.  I'm using Arial and it looks fine on my screen and also from my Android device.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



On 3/9/2011 6:31 PM, Miguel Morales wrote:
Ah, sorry for my brief explanation.
Basically, you have to decide what you want.  
You can either encapsulate your application using Application sub-class which all your activities can then access.
Or, you can have a java class in which you store your global variables IF they don't change at runtime.

So, if you want the first option.  Say you have an Application subclass named MyApp.  You would add it to the manifest file like android:name="MyApp"
Android will use that class which you can then access from your activities using Activity.getApplication() method.
So you can cast that to MyApp like so:

onCreate {
    MyApp app = (MyApp) getApplication();
    MyVar var = app.getVar();
}

If you want the second option, you'd create a class called Consts.java in which you store static global variables.
So something like:
class Consts {
   public final static String AppName = "SOMETHING";
}

Then from your activity you can access it like:

String appname = Consts.AppName

Also, you might want to change your font, it's a bit hard to read your messages.

On Wed, Mar 9, 2011 at 4:24 PM, David Williams <dwil...@dtw-consulting.com> wrote:
Sorry, I am new to Java and Android.

I don't get what you are saying.  I added the following android:name="globalVars" to the manifest.
But I still don't understand how I can get the value of the apiKey, which should be
XXXXXXXXXXXX.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



On 3/9/2011 6:16 PM, Miguel Morales wrote:
If you define to use your custom application class in the android manifest file you can get the class from your activities.

Inside your activity, simple use (MyAppliction) getAppliction() and then you can access your variables.
If it's static global variables you're looking for.  Then you would use MyApplication.MyStaticConst

On Wed, Mar 9, 2011 at 4:11 PM, David Williams <dwil...@dtw-consulting.com> wrote:
Ok, trying to do this but struggling.

I created a class as follows:

package com.dtwconsulting.golfcaddie;

import android.app.Application;

public class globalVars extends Application{
   
    public String apiKey = "XXXXXXXXXXXX";
   
    public String getApiKey() {
        return apiKey;
     }
}

Now, how do I get the value of the apiKey from somewhere else?  Sorry, this is just my lack of knowledge on Java here.  I was hoping it was something like globalVars.getApiKey(), but that doesn't seem to work.

David Williams
Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.



On 3/9/2011 3:24 PM, Chris Stewart wrote:
I tend to do what TreKing suggested.  I have a class called CommonVariables that really holds static strings for Flurry event names, the year parameter for my app (it's seasonal and changes once a year) which is used all over the place, and things like that.

--
Chris Stewart



On Wed, Mar 9, 2011 at 3:53 PM, TreKing <treki...@gmail.com> wrote:
On Wed, Mar 9, 2011 at 2:41 PM, David Williams <dwil...@dtw-consulting.com> wrote:
What is the best way of going about setting up global variables?

IDK about "best", but "easy": public static values somewhere that you set up in a custom Application class.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/ http://www.youtube.com/user/revoltingx
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/ http://www.youtube.com/user/revoltingx
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/ http://www.youtube.com/user/revoltingx
Re: Global Variables Kenny Riddile 3/10/11 7:11 AM
On 3/9/2011 7:35 PM, TreKing wrote:
> On Wed, Mar 9, 2011 at 4:30 PM, Kenny Riddile <kfri...@gmail.com
> <mailto:kfri...@gmail.com>> wrote:
>
>     Assuming the singleton is modifiable via its interface, then for all
>     intents and purposes, yes, they are.
>
>
> Again, no.
>
> Singleton is a design pattern whose purpose is to simplify and control
> access to a an object for which there is and will only be one instance
> of. This instance is set once and used throughout. It does not vary or
> change. It is not variable.
>
> Of course you can change the *state* of the singleton object, if that's
> what you mean by "modifiable", but if you change the *value* of the
> object itself, as one would do with global *variables*, it is no longer
> a singleton.
>
> Even in the sense that the singleton state is modifiable, the whole
> point of the singleton is to provide an interface through which this
> globally accessible object, and its state, is manipulated. There is
> controlled access. This is not the case with your run-of-mill, freely
> accessible global variable.

>
> On Wed, Mar 9, 2011 at 6:11 PM, David Williams
> <dwil...@dtw-consulting.com <mailto:dwil...@dtw-consulting.com>> wrote:
>
>     Ok, trying to do this but struggling.
>
>     I created a class as follows:
>
>
> If all you're doing is defining some *constants* that are not going to
> change, extending Application is way overkill. Especially since casting
> up to your Application type every time you need something makes your
> code horrendous to look at.
>
> You can just do this:
>
> class Constants
> {
>   public static final String KEY = "MyKey";
> }
>
> Then do Constants.Key where you need it. Done.

>
> On Wed, Mar 9, 2011 at 6:11 PM, David Williams
> <dwil...@dtw-consulting.com <mailto:dwil...@dtw-consulting.com>> wrote:
>
>     Sorry, this is just my lack of knowledge on Java here.  I was hoping
>     it was something like globalVars.getApiKey(), but that doesn't seem
>     to work.
>
>
> I highly recommend you brush up on Java, reviewing static and instance
> level access of functions and data in particular.
> -------------------------------------------------------------------------------------------------
> TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago

> transit tracking app for Android-powered devices
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

Perhaps my blanket statement was to simplistic.  Not ALL singletons
"are" global variables.  Also, not all globals are variables (globally
accessible static constants for example).  However, all STATEFUL
singletons are globals, and all stateful singletons with mutable state
(via methods or whatever) are essentially global variables as they
create the same architectural issues.  Limiting my statement to stateful
singletons is kind of irrelevant though, since there's no point in
limiting a stateless type to one instantiation.  Just because a
singleton's state is only mutable by using its interface doesn't change
the fact that you are varying globally accessible state.  When I was
first starting my career, I thought singletons were great (after all, I
read about them in a book!), but experience has taught me that there is
almost always a better way than using mutable global state.  Singletons
essentially get you two things:

  - the guarantee that only one instance can exist
  - global access to that single instance

The second is bad for all the reasons that global variables are
considered bad, and the first is unnecessary.  If you only need one
instance of a class, then just make one.  It really is that simple.
Then give that instance to whomever needs it, instead of letting them
magically pull it from the ether.

Re: [android-developers] Re: Global Variables TreKing 3/10/11 7:41 AM
On Thu, Mar 10, 2011 at 9:11 AM, Kenny Riddile <kfri...@gmail.com> wrote:
Perhaps my blanket statement was to simplistic.  Not ALL singletons "are" global variables.  Also, not all globals are variables (globally accessible static constants for example).  However, all STATEFUL singletons are globals, and all stateful singletons with mutable state (via methods or whatever) are essentially global variables as they create the same architectural issues.

Hmm, perhaps this is semantics at this point. I don't think a singleton is "a global", as it's usually a private member that is statically accessible. However, there is "global access", of course.
 
Just because a singleton's state is only mutable by using its interface doesn't change the fact that you are varying globally accessible state.

No, but in terms of clarity, readability, and ease of debugging, there is a *huge* difference in using a singleton to modify some globally accessible object versus a "classic global".
 
If you only need one instance of a class, then just make one.  It really is that simple. Then give that instance to whomever needs it, instead of letting them magically pull it from the ether.

Normally, I wholeheartedly agree. However, the Android model and intricacies of the lifecycle of an Android app present some unique challenges with regard to managing and passing around objects that are needed across multiple Activities. So it's not always that simple. Sometimes, when considering ease and speed of implementation, readability, and maintainability, a Singleton really is the best option.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices

Re: Global Variables Kenny Riddile 3/10/11 8:03 AM
On 3/10/2011 10:41 AM, TreKing wrote:
> Hmm, perhaps this is semantics at this point. I don't think a singleton
> is "a global", as it's usually a private member that is statically
> accessible. However, there is "global access", of course.

If it's a single piece of state that's accessible globally, then to me,
it's a global, regardless of the particular syntax or representation.

> No, but in terms of clarity, readability, and ease of debugging, there
> is a *huge* difference in using a singleton to modify some globally
> accessible object versus a "classic global".

To me, the third option of "don't use either one" is almost always
better in terms of clarity, readability, and ease of debugging.

> Normally, I wholeheartedly agree. However, the Android model and
> intricacies of the lifecycle of an Android app present some unique
> challenges with regard to managing and passing around objects that are
> needed across multiple Activities. So it's not always that simple.
> Sometimes, when considering ease and speed of implementation,
> readability, and maintainability, a Singleton really is the best option.

I admit, I've only released one Android app of intermediate complexity.
  It's been my experience on other platforms and languages, that any
framework that encourages sections of your code to communicate via
mutable global state is fundamentally flawed.  I haven't felt encouraged
in that manner by Android thus far.

Anyways, this has gotten a bit off-topic from the OP's original
question, and for that, I apologize :)

Re: [android-developers] Re: Global Variables TreKing 3/10/11 8:14 AM
On Thu, Mar 10, 2011 at 10:03 AM, Kenny Riddile <kfri...@gmail.com> wrote:
Anyways, this has gotten a bit off-topic from the OP's original question, and for that, I apologize :)

Thread title: "Global Variables" - seems quite on topic. It's not like we're exchanging cookie recipes =P
Re: [android-developers] Re: Global Variables Kostya Vasilyev 3/10/11 8:19 AM
10.03.2011 19:03, Kenny Riddile пишет:

No, but in terms of clarity, readability, and ease of debugging, there
is a *huge* difference in using a singleton to modify some globally
accessible object versus a "classic global".

To me, the third option of "don't use either one" is almost always better in terms of clarity, readability, and ease of debugging.

What this completely overlooks is that sometimes a globally accessible, mutable, possibly resource-managing, object is the desired semantics.

-- 
Kostya Vasilyev -- http://kmansoft.wordpress.com

Re: Global Variables Indicator Veritatis 3/11/11 12:06 AM
David-

I am surprised anyone can even think to ask your question. The whole
POINT of Java is to provide support for object oriented methodology,
even making object disoriented methodology difficult. Yet you are
asking about the very antithesis of object oriented programming, the
infamous global.

The reason for this is because OOP is based on, among other things,
"information hiding". But by making a variable global, you have done
the very opposite of hiding: you have exposed it to the whole world.
So when you understand why information hiding is so central to OOP,
then you know why globals should be avoided like the plague.

As to how to get "some kind of static value available to all aspects
of your app code", that is not even a coherent idea. But if what you
really mean is more like, "get some kind of static value referenced
directly or indirectly elsewhere in the code, at various levels", then
the answer is: make it private in an object near or at the root of
your hierarchy, so that it is that object's state. Then expose the
state via methods on an as-needed basis. That is, don't  defeat
information hiding by creating public accessors, create instead
methods that use this object's private state to do what other objects
need this object to do.

So, for example, in http://developer.android.com/reference/android/app/Activity.html
Google provides an example, CalendarActivity, which defines the
private SharedPreferences field, mPrefs. It is never used by any other
class; instead, other classes my ask CalendarActiivity to use it to do
something, e.g., onCreate() uses it to set the current view mode.

Another example even more relevant to your "static value" clause:
Android has several classes that exist for the sole purpose of
providing symbolic names for important constants.
DEFAULT_KEYS_DISABLE, DEFAULT_KEYS_SHORTCUT are such examples, in
class Activity.

But notice: strictly speaking, these are still not globals, since they
are in the given class (in the above example, Activity). Nevertheless,
this is the idiomatic way to grant widespread access to static, final
values in Java. Or it was before enum was finally supported;)

On Mar 9, 1:15 pm, David Williams <dwilli...@dtw-consulting.com>
wrote:
> Justin,
>
> Thanks for the information, I will have to look into just what
> subclassing the Application means/is and also SharedPreferences.
>
> That said, why avoid them like the plague?  If you don't use global
> variables then just how do you get some kind of static value (which is
> really what I'm after) available to all aspects of your app code?
>
> TreKing, will look into the public static values as this might be more
> of what I need.
> ------------------------------------------------------------------------
>
> David Williams
> Check out our WebOS mobile phone app for the Palm Pre and Pixi:
> <http://www.dtw-consulting.com/GolfCaddie> Golf Caddie
> <http://www.dtw-consulting.com/GolfCaddie> | Golf Caddie Forum
> <http://www.dtw-consulting.com/GolfCaddie/forum> | Golf Caddie FAQ
> <http://www.dtw-consulting.com/GolfCaddie/faq.html> by DTW-Consulting, Inc.
>
> On 3/9/2011 3:05 PM, Justin Anderson wrote:> The best way is to avoid them like the plague...
>
> > If you can't then you can either subclass Application and make them
> > available there or you can use SharedPreferences...
>
> > Thanks,
> > Justin Anderson
> > MagouyaWare Developer
> >http://sites.google.com/site/magouyaware
>
> > On Wed, Mar 9, 2011 at 1:41 PM, David Williams
> > <dwilli...@dtw-consulting.com <mailto:dwilli...@dtw-consulting.com>>
> > wrote:
>
> >     All,
>
> >     What is the best way of going about setting up global variables?
> >     There will be like 5-6 global variables that I would like to set
> >     when my app is launched that are then available for any code
> >     anywhere in my app.
> >     I did something similar to this under Mojo on WebOS.  I just set
> >     some global variables during the stage-assistant.js script that
> >     could then be used by any script.
>
> >     TIA.
> >     --
> >     ------------------------------------------------------------------------
>
> >     David Williams
> >     Check out our WebOS mobile phone app for the Palm Pre and Pixi:
> >     <http://www.dtw-consulting.com/GolfCaddie> Golf Caddie
> >     <http://www.dtw-consulting.com/GolfCaddie> | Golf Caddie Forum
> >     <http://www.dtw-consulting.com/GolfCaddie/forum> | Golf Caddie FAQ
> >     <http://www.dtw-consulting.com/GolfCaddie/faq.html> by
> >     DTW-Consulting, Inc.
>
> >     --
> >     You received this message because you are subscribed to the Google
> >     Groups "Android Developers" group.
> >     To post to this group, send email to
> >     android-d...@googlegroups.com
> >     <mailto:android-d...@googlegroups.com>
> >     To unsubscribe from this group, send email to
> >     android-developers+unsubscribe@googlegroups.com
> >     <mailto:android-developers%2Bunsubscribe@googlegroups.com>
Re: [android-developers] Re: Global Variables David Williams 3/11/11 1:22 PM
Yes, I got the answer to this question from other people who didn't phrase their answer in a manner like you did.

Thanks though.

David Williams

Check out our WebOS mobile phone app for the Palm Pre and Pixi:
   Golf Caddie | Golf Caddie Forum | Golf Caddie FAQ by DTW-Consulting, Inc.


    <mailto:andro...@googlegroups.com>
    To unsubscribe from this group, send email to     android-developers+unsubscribe@googlegroups.com     <mailto:android-developers%2Bunsubscribe@googlegroups.com>     For more options, visit this group at    http://groups.google.com/group/android-developers?hl=en

        
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

    
Re: [android-developers] Global Variables Brill Pappin 3/11/11 9:15 PM
I'm not positive about this, but I think i just ran into a bug using static vars.

I'm trying to fix it now, but I think my vars are getting GC'ed as the phones runs low on memory and reinitialized when I need them again.
Which of course means that they get reinitialized to the default value when created.
which cases the bug in my app (its a long running app).

Like I said, I'm not yet positive thats the cause of my own trouble, but I think you might want to be careful about storing primitive values in static vars.


Re: [android-developers] Global Variables Brill Pappin 3/11/11 9:16 PM
To clarify, that's *not* an Android or Java bug, but a bug in my own code :)
Re: [android-developers] Global Variables Brill Pappin 3/11/11 9:17 PM
Yah, but those woud be static final :) not global vars, but constants.
Re: [android-developers] Re: Global Variables Brill Pappin 3/11/11 9:21 PM
Singleton is also one to be avoided however... just like global vars :)
However, as we're working on a (sort of) limited device, Singletons are a way of life.
Re: [android-developers] Global Variables Kostya Vasilyev 3/11/11 11:03 PM

What probably happens us that the process gets killed and then relaunched.

The relaunch may execute a code sequence different from when your app is launched for debugging or from the Launcher (i.e. main activity first), and your code that initializes and uses those globals may be sensitive to that.

12.03.2011 8:16 пользователь "Brill Pappin" <br...@pappin.ca> написал:
Re: [android-developers] Re: Global Variables Dianne Hackborn 3/12/11 1:05 AM
Oh good lord, there is nothing intrinsically evil about globals or singletons.  Just don't abuse them.

For example, say I want to do a query on the package manager about a set of applications that are available.  If I stick this as local data inside of a particular activity, then every time the user presses back and returns to the activity the state needs to be reloaded, and if I want to share that state with another activity I need to figure out some way to get the object over to the other activity without using an evil global.

Just use a singleton and a global.  It's the appropriate tool for this.  Like the current code here in Manage Applications, which works 10x better in gingerbread because it was refactored to have a cleanly controlled singleton that tracks everything that needs to be known about the installed applications across all parts of the UI:


On Fri, Mar 11, 2011 at 9:21 PM, Brill Pappin <br...@pappin.ca> wrote:
Singleton is also one to be avoided however... just like global vars :)
However, as we're working on a (sort of) limited device, Singletons are a way of life.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Re: Global Variables Indicator Veritatis 3/12/11 11:42 PM
I get "no such project" when I try to go to your link.

On Mar 12, 1:05 am, Dianne Hackborn <hack...@android.com> wrote:
> Oh good lord, there is nothing intrinsically evil about globals or
> singletons.  Just don't abuse them.
>
> For example, say I want to do a query on the package manager about a set of
> applications that are available.  If I stick this as local data inside of a
> particular activity, then every time the user presses back and returns to
> the activity the state needs to be reloaded, and if I want to share that
> state with another activity I need to figure out some way to get the object
> over to the other activity without using an evil global.
>
> Just use a singleton and a global.  It's the appropriate tool for this.
>  Like the current code here in Manage Applications, which works 10x better
> in gingerbread because it was refactored to have a cleanly controlled
> singleton that tracks everything that needs to be known about the installed
> applications across all parts of the UI:
>
> http://android.git.kernel.org/?p=platform/packages/apps/Settings.git;...
>
> On Fri, Mar 11, 2011 at 9:21 PM, Brill Pappin <br...@pappin.ca> wrote:
> > Singleton is also one to be avoided however... just like global vars :)
> > However, as we're working on a (sort of) limited device, Singletons are a
> > way of life.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-d...@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscribe@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
Re: [android-developers] Re: Global Variables Dianne Hackborn 3/13/11 12:42 AM
That's strange, it works for me.hac...@android.com
Re: Global Variables William Ferguson 3/13/11 3:05 AM
Dianne, I also get 404 "No such project"

On Mar 13, 6:42 pm, Dianne Hackborn <hack...@android.com> wrote:
> That's strange, it works for me.
>
> On Sat, Mar 12, 2011 at 11:42 PM, Indicator Veritatis <mej1...@yahoo.com>wrote:
Re: Global Variables Brill Pappin 3/13/11 10:25 AM
I think Java still has a variant of the GOTO, but I have never seen it
used once in about 18 years of java development!

- Brill Pappin

On Mar 9, 6:01 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
> Oh, come on, guys, it's not that bad :)
>
> Sometimes I even miss a good old-fashioned "goto".
>
> -- Kostya
>
> 10.03.2011 0:26, TreKing ?????:
>
>
>
>
>
>
>
>
>
> > On Wed, Mar 9, 2011 at 3:15 PM, David Williams
> > <dwilli...@dtw-consulting.com <mailto:dwilli...@dtw-consulting.com>>
> > wrote:
>
> >     That said, why avoid them like the plague?
>
> > Global variables are one of those things, like Singletons, that on the
> > surface seem to make life easier, then get abused like a step-child to
> > the point of making everything worse.
>
> > Used correctly, in moderation, it's often the fastest, easiest,
> > cleanest, and most straightforward way of doing something. Like a
> > simple flag indicating DEBUG vs RELEASE, for example.
>
> > --------------------------------------------------------------------------- ----------------------
> > TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
> > transit tracking app for Android-powered devices
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-d...@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscribe@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Kostya Vasilyev --http://kmansoft.wordpress.com
Re: Global Variables Brill Pappin 3/13/11 10:28 AM
public static final apiKey = "xxxxxxx";


now you can access it as:
String key = globalVars.apiKey;

However, not that this is not a variable, its a constant.

According to convention, it should be in upper case:
public static final API_KEY = "xxxxxxx";

- Brill Pappin


On Mar 9, 8:11 pm, David Williams <dwilli...@dtw-consulting.com>
wrote:
> Ok, trying to do this but struggling.
>
> I created a class as follows:
>
> package com.dtwconsulting.golfcaddie;
>
> import android.app.Application;
>
> public class globalVars extends Application{
>
>      public String apiKey = "XXXXXXXXXXXX";
>
>      public String getApiKey() {
>          return apiKey;
>       }
>
> }
>
> Now, how do I get the value of the apiKey from somewhere else?  Sorry,
> this is just my lack of knowledge on Java here.  I was hoping it was
> something like globalVars.getApiKey(), but that doesn't seem to work.
> ------------------------------------------------------------------------
>
> David Williams
> Check out our WebOS mobile phone app for the Palm Pre and Pixi:
> <http://www.dtw-consulting.com/GolfCaddie> Golf Caddie
> <http://www.dtw-consulting.com/GolfCaddie> | Golf Caddie Forum
> <http://www.dtw-consulting.com/GolfCaddie/forum> | Golf Caddie FAQ
> <http://www.dtw-consulting.com/GolfCaddie/faq.html> by DTW-Consulting, Inc.
>
> On 3/9/2011 3:24 PM, Chris Stewart wrote:
>
>
>
>
>
>
>
> > I tend to do what TreKing suggested.  I have a class called
> > CommonVariables that really holds static strings for Flurry event
> > names, the year parameter for my app (it's seasonal and changes once a
> > year) which is used all over the place, and things like that.
>
> > --
> > Chris Stewart
> >http://chriswstewart.com
>
> > On Wed, Mar 9, 2011 at 3:53 PM, TreKing <treking...@gmail.com
> > <mailto:treking...@gmail.com>> wrote:
>
> >     On Wed, Mar 9, 2011 at 2:41 PM, David Williams
> >     <dwilli...@dtw-consulting.com
> >     <mailto:dwilli...@dtw-consulting.com>> wrote:
>
> >         What is the best way of going about setting up global variables?
>
> >     IDK about "best", but "easy": public static values somewhere that
> >     you set up in a custom Application class.
>
> >     --------------------------------------------------------------------------- ----------------------
> >     TreKing <http://sites.google.com/site/rezmobileapps/treking> -
> >     Chicago transit tracking app for Android-powered devices
>
> >     --
> >     You received this message because you are subscribed to the Google
> >     Groups "Android Developers" group.
> >     To post to this group, send email to
> >     android-d...@googlegroups.com
> >     <mailto:android-d...@googlegroups.com>
> >     To unsubscribe from this group, send email to
> >     android-developers+unsubscribe@googlegroups.com
> >     <mailto:android-developers%2Bunsubscribe@googlegroups.com>
Re: Global Variables Brill Pappin 3/13/11 10:37 AM
That is a very good article and entertaining to read.. every one of us
should be reading it and understanding it before we're allowed to call
ourselves programmers :)

- Brill Pappin

On Mar 9, 8:35 pm, fadden <fad...@android.com> wrote:
> On Mar 9, 2:13 pm, TreKing <treking...@gmail.com> wrote:
>
> > On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile <kfridd...@gmail.com> wrote:
> > > Singletons are global variables.
>
> > Uh ... no ... no they're not.
>
> I think this about covers it:
>
>  http://sites.google.com/site/steveyegge2/singleton-considered-stupid
Re: [android-developers] Re: Global Variables Greg Donald 3/14/11 10:43 AM
On Sun, Mar 13, 2011 at 12:25 PM, Brill Pappin <br...@pappin.ca> wrote:
> I think Java still has a variant of the GOTO, but I have never seen it
> used once in about 18 years of java development!

Just because you don't see them doesn't mean they are not there.  They
have always been present in every java binary I've ever decompiled.
Even if your JDK doesn't support GOTO, you can bet your JVM does.


--
Greg Donald
destiney.com | gregdonald.com

More topics »