Re: scope of static?




"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/


.



Relevant Pages

  • Re: Looking for real world examples to explain the difference between procedural (structured?) progr
    ... stateless. ... quaternion support, ... ... obviously you do need a context because you are ... I usually use context structs (as opposed to globals) for most things, ...
    (comp.object)
  • Re: Check if a given function returns something
    ... ->> return is to exit the subroutine at some arbitrary point and the ... naked return is context sensitive. ... vijay's question implies this method isn't ... globals to pass values to/from subs is one of the worst uses of ...
    (perl.beginners)
  • Re: scope of static?
    ... 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. ... Hopefully the JVM does not do any such thing per default for 'static' things in classes it loads and makes several instances of. ... Now if the JVM starts a second instance "A2" of class A, do the java specs guarantee that the static data of the one created first will /not/ be shared or interefere with the static data of the second? ...
    (comp.lang.java.help)
  • Re: Are static variables useful for replacing global variables?
    ... properties of the form object as in ... I don't use a hidden form as a data storage structure, ... That is, they use a form to set globals, and then use ... unless those are variables private to the context ...
    (comp.databases.ms-access)
  • Re: Question about threads?
    ... driver are in arbitrary thread context, ... decided to define the globals. ... You can access your global from your interrupt if it is not in paged memory, ...
    (microsoft.public.win32.programmer.kernel)