Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables and running the same class twice

 
John Smith
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic