Re: scope of static?
- From: "Tony Morris" <not@xxxxxxxxxxx>
- Date: Thu, 13 Apr 2006 01:09:15 GMT
"Jan Wagner" <nospam@xxxxxxxxxx> wrote in message
news:e1ip0b$l6k$1@xxxxxxxxxxxxxxxx
Hi,
i'm still more on the newbie side, and couldn't find any info on this one,
maybe someone can help: when one declares some public method or variable
in a class as static, what is the scope in which the variable really is
static?
The scenario is, I've a gui (awt+swing) app, and it would be very handy to
keep references to a few of the central classes/objects easily accessible
by all current and future classes in that app. Since there is only one
instance of each of these central classes, a single class with static
get/set for each of them could be very useful and severely less messy than
having to hand through references in all class constructors etc.
But, if several instances of the application are started e.g. via
windows/linux command line, will these static get/set methods and
associated static data be shared by all the application instances? Or is
static only really static and inside a single application instance?
Is the behaviour defined somewhere? (i.e., should it work the same way on
all java vm's?)
thanks!
- Jan
A static exists within the scope of a class loader. Quite often, a static is
assumed to be "global" or "across the JVM" or some such. This is incorrect.
In fact, a global variable does not exist until both of the following
conditions are met:
a) the universe is finite in size
b) we learn that the universe is finite in size and control everything
within it
A "global" can only exist "within a known context", therefore "a static is
global within the context of a class loader". One can can even say that "an
instance field is global within the context of an instance". And even more
apparantly absurd, but still correct, "a local declaration is global within
the context of its defined scope".
It is important to understand that by using a "static", you are simply
moving your context to something broader than "the usual" - the class
loader. I have seen "globals" that exist within the context of a JVM; I have
even seen "globals" that exist within the context of "many JVMS", but I have
never seen a "global" that exists within the context of "all JVMs in the
world", let alone "all JVMs in the universe".
Does your entire application exist within this context (the class loader)?
Probably. Does this make your static "global"? Of course not - just define a
new class loader and load it, and you have two hitherto "globals". For your
purposes, assume one class loader in your application, and therefore, a
static is shared data throughout your application. However, as soon as this
assumption falls, so does the notion of your globally shared data - I have
seen many people fall into that trap.
Don't believe the guff - there is plenty of it.
--
Tony Morris
http://tmorris.net/
.
- Follow-Ups:
- Re: scope of static?
- From: Jan Wagner
- Re: scope of static?
- References:
- scope of static?
- From: Jan Wagner
- scope of static?
- Prev by Date: Re: What is Classname$1.class?
- Next by Date: Re: What is Classname$1.class?
- Previous by thread: Re: scope of static?
- Next by thread: Re: scope of static?
- Index(es):
Relevant Pages
|