Friday, March 11, 2011

[android-developers] Re: Global Variables

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-developers@googlegroups.com
> >     <mailto:android-developers@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-developers@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-developers@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

No comments:

Post a Comment