File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Static variables usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static variables usage" Watch "Static variables usage" New topic

Static variables usage

Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
I have a Java program '' 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 '', which reads the value of the static variable 'counter' created in
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

Joined: May 14, 2003
Posts: 67
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.

N 37 33 W 122 18
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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?

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Static variables usage
It's not a secret anymore!