• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

not able to access static data

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is interesting.

I have 2 classes class1 and class2. class1 has method "method1" and class2 has a method "method2".
method1 has a static variable i. method1 is constantly running and always updates i. Now I run method2 parallely with method1.
In method2, when i try to print value of i as "class1.i" , I always get the value "0".
I am expecting method2 to output the current value of i but it is always printing 0(even though method1 is updating the value of i).

Can anyone help me with this?

Thanks,
Chandana.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps if you show the code which causes the error, we could show you the problem.

My guess is that you have a class like this:


Then, you have a second class which tries to access i directly, and find that i is still 0.

When I make a simple example as above can you see why the second class might see only 0?
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, try making the static field volatile. If you're updating and reading it from different threads without synchronization you'll get stale data without the volatile keyword.
 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve and Rob for the immediate reply.

Let me explain the problem more clearly.

I have class "class1" as follows,

public class class1{
static int i;
public static void main(String args[]){
for(;;)
{
i++;
System.out.println("in class1: value of i is"+i);
}
}
}

I have class "class2" as follows,

public class class2{
public static void main(String args[]){
for(int j=0;j<10;j++)
System.out.println("in class2: value of i is"+class1.i);
}
}


Now I open Command prompt, and run class1.
Next I open another command prompt and run class2.
the value of i in class1 changes but the output of class2 is always zero.

There is no concept of threads used in this question.
Can you help me with this?

Chandana.

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now I open Command prompt, and run class1.
Next I open another command prompt and run class2.




When you do this, you are running two different JVMs with two different programs.

Static means that there is one copy per JVM -- not one copy across all JVMs...

Henry
 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.
But what should I do if I want to access a variable across different JVMs? How should I declare a variable that I want to use across all JVMs?

 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would need inter-process communication:
- sockets (or RMI for a higher level protocol)
- files
- database
- ...

That's definitely not a beginner's topic anymore. This problem has been encountered multiple times; a search in the intermediate forum should yield several results.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd have to use some sort of shared memory/messages/etc. system--you're basically saying you want to share memory across multiple applications.
 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob and David. You replies helped me solve my problem.
 
Honk if you love justice! And honk twice for tiny ads!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic