aspose file tools*
The moose likes Java in General and the fly likes Static variables and running the same class twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static variables and running the same class twice" Watch "Static variables and running the same class twice" New topic
Author

Static variables and running the same class twice

John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
Hello,
Suppose I have a class called SomeClass which contains a bunch of static variables and suppose it calls some other classes' (OtherClass1, OtherClass2, etc) static methods which in turn make changes to their static variables. Now, typing at the command line "java SomeClass" will run the main method in SomeClass, one copy of the static variables will be stored in memory, and everything is fine.
Suppose the program runs for quite some time making changes to all the static variables. What happens when I again type "java SomeClass" at the command line while the first one is still running? Do these static variables get their own space, or does this instantiation use the same variables (who knows what the values are now) as the first one? How does the JVM deal with this
, i.e., are the two programs isolated (each has their own set of static variables) or do the static variables/methods intermingle?
Randy
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
Sounds like you're opening a new DOS window to "again type 'java SomeClass' at the command line while the first one is still running" in which case you've started a new JVM and all the classes are reloaded and reinitialized.
John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
Correct. So you're saying each time I run the program, it gets its own copy of static variables? It sounds like I can essentially have two copies of a static variable in memory at the same time but they can never interact, since the JVM is started with a separate instance of the program each time.
Randy
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Randy Gordon:
Hello,
Suppose I have a class called SomeClass which contains a bunch of static variables and suppose it calls some other classes' (OtherClass1, OtherClass2, etc) static methods which in turn make changes to their static variables. Now, typing at the command line "java SomeClass" will run the main method in SomeClass, one copy of the static variables will be stored in memory, and everything is fine.
Suppose the program runs for quite some time making changes to all the static variables. What happens when I again type "java SomeClass" at the command line while the first one is still running? Do these static variables get their own space, or does this instantiation use the same variables (who knows what the values are now) as the first one? How does the JVM deal with this
, i.e., are the two programs isolated (each has their own set of static variables) or do the static variables/methods intermingle?
Randy

1 process can not access the memory of another. static variables and such are tied to the classLoader. If its not the same ClassLoader, it won't see the same static variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static variables and running the same class twice