threads and static members

N

nmlaney

I'm using eclipse to launch classes with a static main and other static
members.

Question: the class takes several minutes to complete, and during that
time I may have updated the class, recompiled, and launched it again.
Will the two classes share the same static values?

I've asked several of my colleagues. Some yes's, some no's. I say no.
It's also been suggested I should refactor the class to not use static
members for configuration parameters.

TIA

Neill
 
O

Oliver Wong

I'm using eclipse to launch classes with a static main and other static
members.

Question: the class takes several minutes to complete, and during that
time I may have updated the class, recompiled, and launched it again.
Will the two classes share the same static values?

You could test this by writing a class that has a single static variable
and just goes in an infinite loop, printing out the contents of that static
variable. Initialize the variable to some value "1", and run it.

Then, edit the class so that now the value is initialized to "2" and run
it. Did the changes affect the first class?
I've asked several of my colleagues. Some yes's, some no's. I say no.
It's also been suggested I should refactor the class to not use static
members for configuration parameters.

It probably depends on whether Eclipse runs programs in the same VM, or
creates a new VM for every program. Even if it's the same VM, might depend
on whether Eclipse fiddles with the ClassLoader (so that two instances of
the class object are loaded, each with their own member variable to
represent your static variable, or if only one instance is loaded).

Since the answer is not immediately apparent (and since it may even
change in future optimizations of the Eclipse platform), you should probably
write your code in such a way so that it doesn't depend on any one specific
behaviour (i.e. don't use static members if it'll be a problem for you).

- Oliver
 
V

Vova Reznik

I'm using eclipse to launch classes with a static main and other static
members.

Question: the class takes several minutes to complete, and during that
time I may have updated the class, recompiled, and launched it again.
Will the two classes share the same static values?

I've asked several of my colleagues. Some yes's, some no's. I say no.
It's also been suggested I should refactor the class to not use static
members for configuration parameters.

TIA

Neill
No. Each run creates new JVM.
 
F

fangsoft.com

no,absolute no. class depends on class loader, Different class loaders
load class in different name space. So JVMs don't share Classes. But in
Jdk1.5 can share Class. You can refer to documents.
 
N

nmlaney

I did as you suggested, and the static members are NOT shared between
launches within eclipse. Thanks to everyone who responded.

Neill
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top