Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Does static has only one instance?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
R Ramesh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic