Re: Static Variables and JAR Files
Knute Johnson wrote:
Jason Cavett wrote:
I am curious - does the scope of static variables carry across
different JAR files?
Here's the issue:
BaseClass is in "BaseClasses.jar"
ExtendedClassA extends BaseClass is in "AnotherPackage.jar"
ExtendedClassB extends BaseClass is in "EvenAnotherPackage.jar"
BaseClass has a static object (ObjectX). Now, normally, this static
object is static across all of the subclasses. However...
These JAR files (AnotherPackage and EvenAnotherPackage) are being read
in by a separate tool. When ExtendedClassA and ExtendedClassB are
used within the context of this tool, ObjectX is instantiated twice
and has two separate values. As far as I can tell, the tool runs
ExtendedClassA and ExtendedClassB within the same JVM, so I am unsure
of what is going on.
Does anybody have any insight into what is going on here? (Sorry for
being vague. I'm not actually working the project, but I'm curious
from an academic standpoint. Another group ran across this problem
today.)
I'm curious how you instantiate a static object? What tool are they using?
The scope of a static class variable is the entire execution of the JVM once
the class is loaded. Its accessibility is that declared for it.
When you say "ObjectX is instantiated twice and has two separate values",
first of all, instance names are supposed to begin with a lower-case letter.
Anyway, do you mean there are two simultaneous instances of
'BaseClass.ObjectX' (we really have to improve these names!)? Or do you mean
there is one variable 'BaseClass.ObjectX' that points to different objects at
different times?
If it's the first case, two simultaneous instances of the "same" static
variable, it's because the class was loaded two different times by two
different ClassLoader instances. That causes the "same" class to actually be
two different classes, one belonging to each ClassLoader.
If it's the second case, the same variable but it points to different objects
at different times, it's because something changed what the variable points to
after it was set the first time.
--
Lew