Scope of static variables

H

Hunter

We all know that a static variable has a single value within an
application no many how many objects are instantiated.

How about if you run two seperate applications of the same code. Is
the variable still static across both applications? Or does each
application maintain it's own objects so static is only global within
the application?

Thanks!
Brett
01 Yamaha R1
99 Honda F4 (Track bike)
89 Katana 750
 
M

Michael Borgwardt

Hunter said:
We all know that a static variable has a single value within an
application no many how many objects are instantiated.

How about if you run two seperate applications of the same code. Is
the variable still static across both applications? Or does each
application maintain it's own objects so static is only global within
the application?

You're misapplying the word "application".

Static variable are unique per virtual machine instance. That means, if
you start two VMs (e.g. type the "java Classname" command twice), the
values in the static variables will be completely independant between
the two.
 
C

Carl Howells

Michael said:
Static variable are unique per virtual machine instance.

Technically, they're unique per ClassLoader. Which can matter
significantly when using something like a servlet container.
 
T

Tony Morris

We all know that a static variable has a single value within an
application no many how many objects are instantiated.

No we don't - this is a common fallacy.
A static field exists once per class.
A class exists once per class loader.
A VM uses classes loaded by one or more class loaders.
An application consists of one or more VMs.

You can figure it out from there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top