Static methods across two same applications in a single JVM

A

astrorail

Hi All,

A question.

I have a JDK installed in my computer. I have build an application
(GUI) MainAppl.java which uses lot of classes which also contains
static methods.

Now I invoke the MainAppl two times. So I have two applications
running in one JVM. Am I correct here?

Now I have different projects loaded in the two applications so
different drawings are drawn in the two frames. (MainAppl class)

Suppose there are static methods which is used by the application.
Does the static method change() is common between the two
applications. So calling change() from one application also affect the
UI of the second application.

I have a feeling that the static instances are common to the two
applications. Or is the two applications share completely different
address spaces?

Any comments would be nice.

Thanks
astrorail.
 
P

P.Hill

astrorail said:
Now I invoke the MainAppl two times. So I have two applications
running in one JVM. Am I correct here?

If in one class you create two MainAppl's then yes there are two instances
in the same space. But how are you running them. Are there also two
threads?
I have a feeling that the static instances are common to the two
applications. Or is the two applications share completely different
address spaces?

Show us how in "invoke" them and I'm sure we'll be able to tell.

-Paul
 
S

srinee

Suppose there is a main JFrame class

class MainClass extends JFrame {
uses some static methods defined in some class
mouseClick() {
StaticMethod.callMe();
}

}
class StaticMethod {

static callMe() {
draw();
}
}

Now I invoke java MainClass file.txt

then I invoke the second application with different file
java MainClass file2.txt

two different drawings are displayed... but later. when i click, the
drawing in the 2nd Frame changes to the first one.
 
P

P.Hill

(e-mail address removed) wrote:

[...]
Now I invoke java MainClass file.txt

then I invoke the second application with different file
java MainClass file2.txt
[...]

Unless you are on an unusual OS, this is two copies of the java
executable. Can't you verify how many "java.exes' are running?
 
T

Tony Morris

A static member is "class-loader wide", not "VM-wide" or "VMs (plural)-wide"
That is, a static member exists once per class-loader.
You have at least 2 class-loaders running each loading their own instance of
the class containing the static member, therefore, your static members are
distinct.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top