Re: Static Variables and JAR Files
Mark Space wrote:
Jason Cavett wrote:
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.
Obviously, when you make a new class via inheritance, it gets it's own
copy of the static variable. So there will be one static for the parent
class, and one for the child class. The static is a "class variable"
and since there are two classes (parent and child) there are two static
variables.
Please predict the output of this simple demonstration, and
then test your prediction:
class Foo {
static int count;
public static void main(String[] unused) {
Foo.count = 1;
Bar.count = 2;
System.out.println("Foo.count = " + Foo.count);
System.out.println("Bar.count = " + Bar.count);
}
}
class Bar extends Foo {
// Mark says a copy of `count' is inherited here
}
If Foo.count and Bar.count are two distinct variables, the
output will show two different values. If it shows the same
value for both, they cannot be distinct. Place your bets!
--
Eric.Sosman@sun.com