Re: [android-developers] Re: Global Variables

2011-03-14 Thread Greg Donald
On Sun, Mar 13, 2011 at 12:25 PM, Brill Pappin  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

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


[android-developers] Re: Global Variables

2011-03-13 Thread Brill Pappin
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  wrote:
> On Mar 9, 2:13 pm, TreKing  wrote:
>
> > On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile  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

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


[android-developers] Re: Global Variables

2011-03-13 Thread Brill Pappin
public static final apiKey = "xxx";


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 = "xxx";

- Brill Pappin


On Mar 9, 8:11 pm, David Williams 
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 = "";
>
>      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
> >http://chriswstewart.com
>
> > On Wed, Mar 9, 2011 at 3:53 PM, TreKing  > > wrote:
>
> >     On Wed, Mar 9, 2011 at 2:41 PM, David Williams
> >      >     > 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-developers@googlegroups.com
> >     
> >     To unsubscribe from this group, send email to
> >     android-developers+unsubscr...@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-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Global Variables

2011-03-13 Thread Brill Pappin
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  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
> > 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  - 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-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Kostya Vasilyev --http://kmansoft.wordpress.com

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


[android-developers] Re: Global Variables

2011-03-13 Thread William Ferguson
Dianne, I also get 404 "No such project"

On Mar 13, 6:42 pm, Dianne Hackborn  wrote:
> That's strange, it works for me.
>
> On Sat, Mar 12, 2011 at 11:42 PM, Indicator Veritatis 
> wrote:
>
>
>
> > I get "no such project" when I try to go to your link.
>
> > On Mar 12, 1:05 am, Dianne Hackborn  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  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-developers@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > android-developers+unsubscr...@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
>
> > > 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.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@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
>
> 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.

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


Re: [android-developers] Re: Global Variables

2011-03-13 Thread Dianne Hackborn
That's strange, it works for me.

On Sat, Mar 12, 2011 at 11:42 PM, Indicator Veritatis wrote:

> I get "no such project" when I try to go to your link.
>
> On Mar 12, 1:05 am, Dianne Hackborn  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  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-developers@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-developers+unsubscr...@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
> >
> > 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.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@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

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.

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

[android-developers] Re: Global Variables

2011-03-12 Thread Indicator Veritatis
I get "no such project" when I try to go to your link.

On Mar 12, 1:05 am, Dianne Hackborn  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  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-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@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
>
> 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.

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


Re: [android-developers] Re: Global Variables

2011-03-12 Thread Dianne Hackborn
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;a=blob;f=src/com/android/settings/applications/ApplicationsState.java;h=6e9f2a2d296ac349e6acde81312485adae5dd351;hb=HEAD#l324

On Fri, Mar 11, 2011 at 9:21 PM, Brill Pappin  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-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@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

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.

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

Re: [android-developers] Re: Global Variables

2011-03-11 Thread Brill Pappin
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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Re: Global Variables

2011-03-11 Thread David Williams
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.




On 3/11/2011 3:06 AM, Indicator Veritatis wrote:

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
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:
  Golf Caddie
  | Golf Caddie Forum
  | Golf Caddie FAQ
  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
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:
   Golf Caddie
   | Golf Caddie Forum
   | Golf Caddie FAQ
   by
 DTW-Consulting, Inc.
 --
 You received this message because y

[android-developers] Re: Global Variables

2011-03-11 Thread Indicator Veritatis
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 
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:
>  Golf Caddie
>  | Golf Caddie Forum
>  | Golf Caddie FAQ
>  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
> > 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:
> >      Golf Caddie
> >      | Golf Caddie Forum
> >      | Golf Caddie FAQ
> >      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-developers@googlegroups.com
> >     
> >     To unsubscribe from this group, send email to
> >     android-developers+unsubscr...@googlegroups.com
> >     
> >     For more options, visit this group at
> >    http://groups.goo

Re: [android-developers] Re: Global Variables

2011-03-10 Thread Kostya Vasilyev

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


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

Re: [android-developers] Re: Global Variables

2011-03-10 Thread TreKing
On Thu, Mar 10, 2011 at 10:03 AM, Kenny Riddile  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

-
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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: Global Variables

2011-03-10 Thread Kenny Riddile

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 :)


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


Re: [android-developers] Re: Global Variables

2011-03-10 Thread TreKing
On Thu, Mar 10, 2011 at 9:11 AM, Kenny Riddile  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

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

[android-developers] Re: Global Variables

2011-03-10 Thread Kenny Riddile

On 3/9/2011 7:35 PM, TreKing wrote:

On Wed, Mar 9, 2011 at 4:30 PM, Kenny Riddile mailto:kfridd...@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
mailto:dwilli...@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
mailto:dwilli...@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

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@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.


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


Re: [android-developers] Re: Global Variables

2011-03-09 Thread Justin Anderson
I agree with TreKing.

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


On Wed, Mar 9, 2011 at 5:35 PM, TreKing  wrote:

> On Wed, Mar 9, 2011 at 4:30 PM, Kenny Riddile  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 <
> dwilli...@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 <
> dwilli...@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
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Re: Global Variables

2011-03-09 Thread TreKing
On Wed, Mar 9, 2011 at 4:30 PM, Kenny Riddile  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  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  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

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

[android-developers] Re: Global Variables

2011-03-09 Thread fadden
On Mar 9, 2:13 pm, TreKing  wrote:
> On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile  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

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


Re: [android-developers] Re: Global Variables

2011-03-09 Thread Kostya Vasilyev
What if the Singleton is a Facade or a Mediator? :)
10.03.2011 1:44 пользователь "Scott Davies"  написал:
>
>
> On 11-03-09 5:30 PM, "Kenny Riddile"  wrote:
>
>>On 3/9/2011 5:13 PM, TreKing wrote:
>>> On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile >> > wrote:
>>>
>>> Singletons are global variables.
>>>
>>>
>>> Uh ... no ... no they're not.
>>>
>>>
>>>-
>>>
>>> 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-developers@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> android-developers+unsubscr...@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.
>
> 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
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Re: Global Variables

2011-03-09 Thread Scott Davies


On 11-03-09 5:30 PM, "Kenny Riddile"  wrote:

>On 3/9/2011 5:13 PM, TreKing wrote:
>> On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile > > wrote:
>>
>> Singletons are global variables.
>>
>>
>> Uh ... no ... no they're not.
>>
>> 
>>-
>>
>> 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-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscr...@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.

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


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


[android-developers] Re: Global Variables

2011-03-09 Thread Kenny Riddile

On 3/9/2011 5:13 PM, TreKing wrote:

On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile mailto:kfridd...@gmail.com>> wrote:

Singletons are global variables.


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

-
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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@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.


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


Re: [android-developers] Re: Global Variables

2011-03-09 Thread TreKing
On Wed, Mar 9, 2011 at 4:10 PM, Kenny Riddile  wrote:

> Singletons are global variables.


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

-
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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: Global Variables

2011-03-09 Thread Kenny Riddile

On 3/9/2011 4:26 PM, TreKing wrote:

On Wed, Mar 9, 2011 at 3:15 PM, David Williams
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  - 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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Singletons are global variables.

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


[android-developers] Re: Global Variables

2011-03-09 Thread Kenny Riddile

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:
 Golf Caddie
 | Golf Caddie Forum
 | Golf Caddie FAQ
 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-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@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.

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


[android-developers] Re: Global variables

2009-03-28 Thread Tseng

Unlike in C/C++ or PHP you can't have global variables in Java (or
C#).

The closest thing to a global is a public static variable.

for example

class YourActivity extends Activity {
   public static boolean bMyBoolean;
}

Now you could access it with

if(YourActivity.bMyBoolean) {
   // Do something
}

However be carefull not to save any objects which holds a reference to
the context of your activity, as this could cause some serious memory
leaks
It is possible to set/change the value and it's accessible from
anywhere
On Mar 28, 9:33 am, Zhubham  wrote:
> Hi,
>
> Can anyone hep me with how to make a (boolean) variable global in
> Android?
>
> Thanks.
>
> Best Regards,
> Zhubham
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---