Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables usage

 
Md Fizal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java program 'Test1.java' which creates a static variable 'counter' and initializes it to 100. Later in 'Test1', the value of 'counter' gets updated to '200.
I have another program 'Test2.java', which reads the value of the static variable 'counter' created in Test1.java.
Problem here is that, I am getting the value of 'counter' as 100 always, and not the updated value. What should I do to get the updated value of the static variable from Program 2?
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'm not sure I completely understand your situation. Do you run the two programs separately? Or are both classes loaded into the same JVM together (like from the same .jar)?
Your usage of 'program' may be throwing me off and you may be meaning you have two classes that are in the same program.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fiz,
Welcome to JavaRanch, the absolute best site on the www for Java information. We don't have many rules around here, but we do have one. Please change your display name to a first and last name to comply with the JavaRanch Naming Policy. You can change it here. Thank you for your cooperation.
Try posting the code so we can see what's going on. Be sure to use the code button and place your code between the tags.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "Later in 'Test1', the value of 'counter' gets updated to '200." - how is that actually coded?
It is possible that you are setting a different variable with the same name and not realizing it.
Can you show us the code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic