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
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?