• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables values are not getting refreshed

 
pendse anagha
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
I am facing a problem for which I dont seem to have an answer to .

I have a class in which I have defined a static variable ( not final )
The value of this variable comes from a method
Here is the code :


The HashMap that I am using contains key value pairs which come from the database

The first time I run this code it runs fine
I get the value correctly in any other class when I refer this static variable - "test"

However if some value changes in the database - then that key and value get replaced in the hashmap

The problem is even though the values have changed in the hashmap
(I have printed the hashmap contents which shows the changed values )

if I use the variable - "test" it still gives me the old values

I tried putting in a System.out.println in the method - "getTestValue"
The first time it prints fine
On subsequent reference to the static variable - the right hand side i.e the method just does not get called

No wonder I do not see the changed value.


But why does the method not get executed ?

Please help

Regards ,
-anagha
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"test" is not a method. It's a static variable. The initializer (the call to getTestValue()) will be executed once, when the class is first loaded; after that, it will never be executed again.

Instead of using the variable "test", your code should call getTestValue() directly. "test" serves no purpose and can be deleted.
 
pendse anagha
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest for your prompt help.

Could you kindly elaborate or point me as to where I could look up and get a better understanding as to why the static method never gets invoked again ?

Apologies if this seems to be too basic a question.

Regards ,
-anagha
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anagha,
Can you please post the complete code? The code above don't say how you are updating the map. It will be better to explain with code.

What Earnest saying is the variable test is a class variable. It is static too. That means test will be initialized when you load the class.

We are not saying any other call to getTestValue(String key). Assuming you are calling the method only once consider the following scenario.

[ December 07, 2005: Message edited by: jiju ka ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pendse anagha:

Could you kindly elaborate or point me as to where I could look up and get a better understanding as to why the static method never gets invoked again ?


It sounds like you're expecting "test" to be a kind of macro or abbreviation or nickname for calling the getTestValue() function, but it's not. In Java, a variable is just a bit of memory that can hold a value. No variable's value ever changes all by itself: some code somewhere would have to explicitly set test to the new value. In particular, whatever code updated that HashMap would have to know to also update "test".

Why do you want your other code to use "test" rather than calling the "getTestValue()" function, which is the normal way to do things?
 
pendse anagha
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo !!!
Yes - I was somehow expecting that on subsequent references to the variable - since it has a method call on RHS - I was expecting it to invoke that method everytime !!!

It now makes sense - it is just a variable - and once it has a value assigned to it
- its value wouldnt change unless explicitly changed

Would you call this bad coding practice ?

I was definitely confused.

Here is a bit more elaborate code
However thanks Ernest - my doubts are indeed clarified with what you rightly point out.

Thank you once more.

Class that looks for changed values in the database and which populates the HashMap
This class runs every 10 minutes and refreshes the HashMap

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic