• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

JVM Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A question that an interviewer put forward.

1. Consider there are 2 JVMs on ONE machine. Consider also that you have a Class that has a static variable.
2. Now consider that you are initiate the static variable via the main method of that class on one JVM.
3. If I display the values of the class' static variable on the second JVM, what value will I get? Will it be the same value that I had initiated using the first JVM or will it be the value specific to the JVM in question where Im running the programs/classes.

Its a pure application. SOmething as simple as a HelloWorld program.

Any comments/answers would be welcome.

Thanx,
Nidhin.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it returns the first value of the static variable.when you
have not initialised it, the default value 0 will be displayed.

-Anandh
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sue I understand the question, but separate JVMs do not share the same memory space. Thus, the values of the variables are independent. Try the following:


Run this class as : java HelloWorld
Then, while it's sleeping (it's sleep for an hour -- you'll have the time), launch another: java HelloWorld Print

You will get "Hello, World" printed, even though the first program set it to "Bonjour, Monde" -- the variables are separate.
 
Nidhin Kannan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
I'm not sue I understand the question, but separate JVMs do not share the same memory space. Thus, the values of the variables are independent. Try the following:


Run this class as : java HelloWorld
Then, while it's sleeping (it's sleep for an hour -- you'll have the time), launch another: java HelloWorld Print

You will get "Hello, World" printed, even though the first program set it to "Bonjour, Monde" -- the variables are separate.



Hi Joel,
thanx for the reply. this programe has this argument and no argument methods to call the HelloWorld. thats bound to give different results even on the same JVM.
However, i think the results should be same because u cant have 2 jvms sharing the same memory on the same CPU based machine, meaning your copy of the program would have initiated to the same value in case of the static variables.
Whatsay??
Nidhin. your copy of the program
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still not sure I understand the situation that you are describing -- perhaps the best way to answer the question is to set it up yourself and give it a quick test.

Anyway, based on you statements, I'll propose this code:


Now, run "java HelloWorld" and get "Bonjour, Monde!" as the result.
While that JVM is running, start another with:
"java Test" and get "Hello, World!" as the result.

That's 2 JVMs with two separaet memory spaces. Thus, changing the value of the static variable in one does not affect the other.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer lies in how you do this test.

If you launch the first program in a command window, you won't get another command prompt until the first program finishes. So you have to open another command window to launch the second program. That exists in a separate address space, with no access to application data, including static variables, in the first address space.

I'm sure there is system software around that can tie the two jvm's together, but the standard jvm's don't share their variables between address spaces.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are my views, the JVM runs within an environment and the address space is unique , thus what ever would be declared under the context of one JVM would not be valid on the others context.
Thus this should clearify your thoughts as to what answer you should have given.


Thanks
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see how many interesting ways we can all say the same thing!

Two JVMs running on the same machine are no different than two JVMs running on different machines as far as static variables are concerned.

Of course, this holds for all objects and memory spaces. You can get a similar effect with regard to separate static "pools" in a single JVM using different class loaders. This is what J2EE containers do with their WAR and EAR class loader hierarchies. That gets truly messy.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic