"Global" variables using static class

Discussion in 'Java' started by Mike Smith, Jun 24, 2004.

  1. Mike Smith

    Mike Smith Guest

    I have an AWT applet (no Swing) which passes around an instance of
    applet-wide "global" variables as we called them in a past life. It
    occurred to me I could simply make it a static class with all static
    variables and avoid passing around this instance everywhere. For
    example instead of this:

    public class MyGlobals {
    public int myGlobal1;
    public int myGlobal2;
    public String myGlobal3;
    <etc>
    public MyGlobals {
    <intit code goes here>
    }
    }

    I would do this instead:

    public class MyGlobals {
    public static int myGlobal1;
    public static int myGlobal2;
    public static String myGlobal3;
    <etc>
    static{
    <intit code goes here>
    }
    }

    The advantage is that I would not have to make an instance of MyGlobal
    and pass it as a parameter to every God-forsaken method that needs it.
    Instead I would simply reference it as MyGlobals.<var name> and let
    the class loader deal with it. Please, no lectures on "thinking in
    Java vs thinking in whatever", application-wide variables exist and
    you have to deal with them. The question is what is the disadvantage
    with regard to efficiency. Is invoking the class loader each time you
    access a non-final static var a big hit?

    Mike Smith
     
    Mike Smith, Jun 24, 2004
    #1
    1. Advertising

  2. Mike Smith wrote:
    <snip>
    > The advantage is that I would not have to make an instance of MyGlobal
    > and pass it as a parameter to every God-forsaken method that needs it.
    > Instead I would simply reference it as MyGlobals.<var name> and let
    > the class loader deal with it. Please, no lectures on "thinking in
    > Java vs thinking in whatever", application-wide variables exist and
    > you have to deal with them. The question is what is the disadvantage
    > with regard to efficiency. Is invoking the class loader each time you
    > access a non-final static var a big hit?


    I don't think this would hit the calssloader repeatedly, but just the
    once, the first time you refer to the class.

    The only disadvantage I can think of is that you might one day want to
    make this thing you are writing just a component of a larger system,
    or run multiple instances of it in the same VM, in which case the
    global values may no longer be quite so global. That has happened
    to me just once, where I decided to incorporate several disparate
    utility services into one glorious multi-service program.

    Steve
     
    Steve Horsley, Jun 24, 2004
    #2
    1. Advertising

  3. Mike Smith

    Bryce Guest

    On 24 Jun 2004 13:11:55 -0700, (Mike Smith)
    wrote:

    >The question is what is the disadvantage
    >with regard to efficiency. Is invoking the class loader each time you
    >access a non-final static var a big hit?


    Static variables are loaded when the class is first loaded. Just once,
    so I doubt its any hit...

    --
    now with more cowbell
     
    Bryce, Jun 24, 2004
    #3
  4. Mike Smith

    Mike Smith Guest

    > "Steve Horsley" <> wrote
    > The only disadvantage I can think of is that you might one day want to
    > make this thing you are writing just a component of a larger system,
    > or run multiple instances of it in the same VM, in which case the
    > global values may no longer be quite so global. That has happened
    > to me just once, where I decided to incorporate several disparate
    > utility services into one glorious multi-service program.
    >

    Hmmm, in a perfect world I could access the static vars via getters and
    setters and the compiler would inline the calls. That would minimize the
    hit should I decide to go the multiple instance route down the road. Any
    idea if the compiler (any) does this? I'm such a wimp anymore, in my C++
    days I always knew exactly what machine code was produced by which compiler
    for which target.

    Mike
     
    Mike Smith, Jun 24, 2004
    #4
  5. Mike Smith

    Tony Morris Guest

    "Mike Smith" <> wrote in message
    news:...
    > I have an AWT applet (no Swing) which passes around an instance of
    > applet-wide "global" variables as we called them in a past life. It
    > occurred to me I could simply make it a static class with all static
    > variables and avoid passing around this instance everywhere. For
    > example instead of this:
    >
    > public class MyGlobals {
    > public int myGlobal1;
    > public int myGlobal2;
    > public String myGlobal3;
    > <etc>
    > public MyGlobals {
    > <intit code goes here>
    > }
    > }
    >
    > I would do this instead:
    >
    > public class MyGlobals {
    > public static int myGlobal1;
    > public static int myGlobal2;
    > public static String myGlobal3;
    > <etc>
    > static{
    > <intit code goes here>
    > }
    > }
    >
    > The advantage is that I would not have to make an instance of MyGlobal
    > and pass it as a parameter to every God-forsaken method that needs it.
    > Instead I would simply reference it as MyGlobals.<var name> and let
    > the class loader deal with it. Please, no lectures on "thinking in
    > Java vs thinking in whatever", application-wide variables exist and
    > you have to deal with them.


    I'm not going to but I'm inclined to suggest you seriously rethink what you
    are doing.
    There are much nicer ways of achieving what you are trying to do.
    In fact, I'd be inclined to suggest that what you are trying to do is never
    justified (not even
    in an extreme circumstance).

    (lecture omitted for brevity).

    > The question is what is the disadvantage
    > with regard to efficiency. Is invoking the class loader each time you
    > access a non-final static var a big hit?


    eh?
    This misunderstanding undermines your reasoning for the pros and cons of
    your (clearly flawed) approach - since you seem to be misunderstanding what
    the class loader is doing for you when a static member is accessed.

    Side note: A static final does not imply a compile-time constant - there are
    other requirements (JLS 15.28).

    Suggest you:
    a) read up on what a Java class loader is and how it works
    b) read up on general software development techniques
    c) reconsider your approach.
    in that order

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Jun 24, 2004
    #5
  6. Mike Smith

    Mike Smith Guest

    > ----- Original Message -----
    > From: "Tony Morris" <>
    > I'm not going to but I'm inclined to suggest you seriously rethink what

    you
    > are doing.
    > There are much nicer ways of achieving what you are trying to do.
    > In fact, I'd be inclined to suggest that what you are trying to do is

    never
    > justified (not even
    > in an extreme circumstance).
    >
    > (lecture omitted for brevity).


    Thanks for ommiting the lecture but I really am interested in your solution.
    I have a class 1 which calls a method in class 2 which call a method in
    class 3 on to a method in say, class 20. In an extreme case none of the
    classes in between require the "globals" class. But they all have to pass
    it along just so class 20 has access. There are System-wide static classes
    in Java which can be accesed anywhere, why not application-wide static
    classes?

    Mike Smith
     
    Mike Smith, Jun 25, 2004
    #6
  7. "Mike Smith" <> wrote in message
    news:eBJCc.1043$...
    > Thanks for ommiting the lecture but I really am interested in your

    solution.
    > I have a class 1 which calls a method in class 2 which call a method in
    > class 3 on to a method in say, class 20. In an extreme case none of the
    > classes in between require the "globals" class. But they all have to pass
    > it along just so class 20 has access. There are System-wide static

    classes
    > in Java which can be accesed anywhere, why not application-wide static
    > classes?
    >
    > Mike Smith
    >


    Is another option to use a type of Registry
    http://martinfowler.com/eaaCatalog/registry.html object?

    Shane


    --
    “There’s no point in being grown up if you can’t be childish sometimes” -
    Doctor Who
     
    Shane Mingins, Jun 25, 2004
    #7
  8. Mike Smith

    Tony Morris Guest

    "Mike Smith" <> wrote in message
    news:eBJCc.1043$...
    > > ----- Original Message -----
    > > From: "Tony Morris" <>
    > > I'm not going to but I'm inclined to suggest you seriously rethink what

    > you
    > > are doing.
    > > There are much nicer ways of achieving what you are trying to do.
    > > In fact, I'd be inclined to suggest that what you are trying to do is

    > never
    > > justified (not even
    > > in an extreme circumstance).
    > >
    > > (lecture omitted for brevity).

    >
    > Thanks for ommiting the lecture but I really am interested in your

    solution.
    > I have a class 1 which calls a method in class 2 which call a method in
    > class 3 on to a method in say, class 20. In an extreme case none of the
    > classes in between require the "globals" class. But they all have to pass
    > it along just so class 20 has access. There are System-wide static

    classes
    > in Java which can be accesed anywhere, why not application-wide static
    > classes?
    >


    Even so, this is not a justification for declaring "application-wide" [sic]
    data.
    You might consider passing a callback, so that there is only one parameter
    which is passed.
    You might consider have a singleton that contains your "application-wide"
    context.
    There are many possibilities, but without a better context, I'm apprehensive
    to suggest any.
    If your methods need this context, it should be passed, regardless of
    whether or not the intervening methods "require" it i.e. explicitly use it
    (they do, in fact, require it to call the methods that perform the intended
    task).

    Be aware that a static is not "system-wide", nor "application-wide", not
    even "JVM-wide".
    It is, in fact, "class-loader wide".

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Jun 25, 2004
    #8
  9. Mike Smith

    Mike Smith Guest

    "Tony Morris" <> wrote in message
    news:cbg51h$9ai$...
    > Even so, this is not a justification for declaring "application-wide"

    [sic]
    > data.
    > You might consider passing a callback, so that there is only one parameter
    > which is passed.
    > You might consider have a singleton that contains your "application-wide"
    > context.
    > There are many possibilities, but without a better context, I'm

    apprehensive
    > to suggest any.
    > If your methods need this context, it should be passed, regardless of
    > whether or not the intervening methods "require" it i.e. explicitly use it
    > (they do, in fact, require it to call the methods that perform the

    intended
    > task).
    >

    The context is that of a very graphics oriented app. The global variables
    are things like a boolean called "isApplet" for doing things differently if
    running as an applet, or "hostWindow" for centering dialogs and alerts on
    the main window and such. I think the singleton idea is good, especially
    since it lifts the restriction of initializing everything when the class
    loads. Thanks,

    Mike
     
    Mike Smith, Jun 25, 2004
    #9
  10. Mike Smith

    Stefan Poehn Guest

    "Mike Smith" <> schrieb im Newsbeitrag
    news:HsHCc.985$...
    > > "Steve Horsley" <> wrote

    > [...]
    > idea if the compiler (any) does this? I'm such a wimp anymore, in my C++
    > days I always knew exactly what machine code was produced by which

    compiler
    > for which target.
    >


    What is it good for to know the machine code? Even if you understand
    (optimized or non-optimized) machine code, I seriously doubt that would be
    of any help. Your machine code depends on so many layers (processor,
    chipset, operating system), that it is of no interest if there is still
    another layer (java virtual machine).

    > Mike
    >
    >
     
    Stefan Poehn, Jun 25, 2004
    #10
  11. Mike Smith

    Mike Smith Guest

    "Stefan Poehn" <> wrote in message
    news:cbh1br$1m2m$...
    > What is it good for to know the machine code? Even if you understand
    > (optimized or non-optimized) machine code, I seriously doubt that would be
    > of any help. Your machine code depends on so many layers (processor,
    > chipset, operating system), that it is of no interest if there is still
    > another layer (java virtual machine).
    >

    Yes, you are correct. Even if the Java compiler is doing something
    inefficient the JIT compiler has a second chance to optimize it on the way
    to the target machine code. Thanks for reminding me, I will try harder to
    not care :)

    Mike
     
    Mike Smith, Jun 25, 2004
    #11
  12. Mike Smith

    Roedy Green Guest

    On Fri, 25 Jun 2004 13:12:59 +0200, "Stefan Poehn"
    <> wrote or quoted :

    >
    >What is it good for to know the machine code?


    The precise details of the machine code for your machine is of less
    importance than general understanding of how Von Neumann machines work
    inside. If you have written any assembler, you have a good intuition
    that file open is a much more time consuming operation than say
    throwing an exception.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 25, 2004
    #12
  13. Mike Smith

    Stefan Poehn Guest

    "Mike Smith" <> schrieb im Newsbeitrag
    news:gjYCc.77321$...
    >
    > "Stefan Poehn" <> wrote in message
    > news:cbh1br$1m2m$...
    > > What is it good for to know the machine code? Even if you understand
    > > (optimized or non-optimized) machine code, I seriously doubt that would

    be
    > > of any help. Your machine code depends on so many layers (processor,
    > > chipset, operating system), that it is of no interest if there is still
    > > another layer (java virtual machine).
    > >

    > Yes, you are correct. Even if the Java compiler is doing something
    > inefficient the JIT compiler has a second chance to optimize it on the way
    > to the target machine code. Thanks for reminding me, I will try harder to
    > not care :)
    >


    The other option is to waste years on finding bugs while watching bits. I am
    not keen on doing that, but that should not prevent you from reading the
    many ones and zeroes that fly around in your computer.

    > Mike
    >
    >
     
    Stefan Poehn, Jun 28, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,408
    Tony Morris
    Oct 19, 2004
  2. jose luis fernandez diaz

    Nonlocal Store (global and class static variables)

    jose luis fernandez diaz, Jan 2, 2004, in forum: C++
    Replies:
    8
    Views:
    1,226
    Victor Bazarov
    Jan 6, 2004
  3. Replies:
    3
    Views:
    925
    Thomas Matthews
    Sep 9, 2006
  4. Mark
    Replies:
    2
    Views:
    425
    Jim Langston
    Oct 16, 2007
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,378
    James Kanze
    Dec 19, 2008
Loading...

Share This Page