M
Mike Smith
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
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