This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Does static has only one instance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does static has only one instance?" Watch "Does static has only one instance?" New topic
Author

Does static has only one instance?

R Ramesh
Greenhorn

Joined: Oct 08, 2004
Posts: 9
Hello All,

If I declare a variable as static, then I thought only ONE copy of the variable exits in the JVM.
But pls look at the below pgm, here in static2 class i declared a string.

package com.sample;
public class Static2 {
public static String str = "PARIS";
}

From below pgm i try to change the value and checked whether str in Staic2 class is changed. It is working perfect.

package com.sample;
public class Static1 {
Public static void main(String[] args) throws InterruptedException {
System.out.println("Str is :"+Static2.str);
Static2.str = "ROME";
System.out.println("After changing Str is :"+Static2.str);
Thread.sleep(5000);
System.out.println("After SLEEP Str is :"+Static2.str);
}
}


Now when I have a third pgm, as below, Here the output is "PARIS"
package com.sample;

public class Static3 {
public static void main(String[] args) { System.out.println("Str is :"+Static2.str);
}
}

My question is why the output is "PARIS", it should be as "ROME". Since i declared the variable str as static, it should contain ONLY one copy of variable isn't?

Pls tell me where i did wrong.

Thanks a lot in advance.
R Ramesh
Greenhorn

Joined: Oct 08, 2004
Posts: 9
Hello All,

I just forget to say one thing,

Here i start the static3.java program before the Static1.java program execution ends. Meaning that before the 5 secs sleep, I invoke the Static3.java program.

Pls help me...thanx
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
How are you running the programs - from the command line? Then you have two JVM instances running, not one, and thus two separate and different Static2.str fields.


Ping & DNS - my free Android networking tools app
R Ramesh
Greenhorn

Joined: Oct 08, 2004
Posts: 9
Thanks for your reply.

Yes I run the program in command line.
So does it means, when every class is invoked separately, then separate JVM instance is created for new invoking of a class?

From my earlier program, could you please tell how i can change the value of the instance variable from the separate jvm instance?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
You will have a new JVM instance for each "java" process that you start.

There are a number of ways you could implement interprocess-communication between two JVMs - sockets probably have the lowest overhead.
R Ramesh
Greenhorn

Joined: Oct 08, 2004
Posts: 9
Thanks Ulf Dittmer for your support.

I have all this 3 program in the same machine. I believe that we have use socket connection ONLY if we want to call another program running in different machine.

But here, from one command prompt i call first Static1.java and from the another cmd prompt i will call Static3.java.
How I can make sure to get the latest changed value when I run Static3.java program.

-thanx
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
By using interprocess communication - sockets are one way to do that. It matters not whether the two processes are on the same machineor on different machines. While there are any number of methods (files, databases, RMI, web services, JMS, email, ...), sockets have the benefit of not needing any external infrastructure. All you need to use them is already built into the JVM.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

only ONE copy of the variable exits in the JVM.

A common misunderstanding.
There is one to one relationship from class loader to static variable - not from JVM to static variable. If there happens to be a one to one relationship from class loader to JVM, then the indirect one to one relationship from JVM to static variable is purely consequential.


Tony Morris
Java Q&A (FAQ, Trivia)
 
Consider Paul's rocket mass heater.
 
subject: Does static has only one instance?