Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > "Global" variables using static class

Reply
Thread Tools

"Global" variables using static class

 
 
Mike Smith
Guest
Posts: n/a
 
      06-24-2004
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
 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      06-24-2004
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
 
Reply With Quote
 
 
 
 
Bryce
Guest
Posts: n/a
 
      06-24-2004
On 24 Jun 2004 13:11:55 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      06-24-2004
> "Steve Horsley" <(E-Mail Removed)> 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


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      06-24-2004
"Mike Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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.2.

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



 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      06-24-2004
> ----- Original Message -----
> From: "Tony Morris" <(E-Mail Removed)>
> 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


 
Reply With Quote
 
Shane Mingins
Guest
Posts: n/a
 
      06-25-2004
"Mike Smith" <(E-Mail Removed)> wrote in message
news:eBJCc.1043$(E-Mail Removed) ...
> 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


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      06-25-2004

"Mike Smith" <(E-Mail Removed)> wrote in message
news:eBJCc.1043$(E-Mail Removed) ...
> > ----- Original Message -----
> > From: "Tony Morris" <(E-Mail Removed)>
> > 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


 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      06-25-2004
"Tony Morris" <(E-Mail Removed)> wrote in message
news:cbg51h$9ai$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Stefan Poehn
Guest
Posts: n/a
 
      06-25-2004
"Mike Smith" <(E-Mail Removed)> schrieb im Newsbeitrag
news:HsHCc.985$(E-Mail Removed). ..
> > "Steve Horsley" <(E-Mail Removed)> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
referring to static fields of enclosing class from static inner class? bugbear Java 4 08-23-2006 08:26 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM



Advertisments