• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Private Static variable  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one of the Jar files, we have the class file like this...



Due to this, the variable "allEmployees" will get the data from the database only for the first time of invocation of the method "getEmployees". From the second time onwards, the data will be stored permanently in the variable "allEmployees" and will be returned directly. It will not go inside the IF loop from the second time onwards. Due to this, if any new Employeed are added in the database, it is not fetching the data from the database. It is returning the previously stored data only. We need to restart the application each time, to make the variable "allEmployees" value null. Is it possible to access this variable from another class and forcibly make this varible null, so that it will go inside the if condition and will fetch the data from the database when ever we required
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just add a method to MyClass to set the allEmployees variable to null, so that next time when you call getEmployees() it will fetch them from the database again?

Hacking to change the value of a private variable is possible using the reflection API, but it's not a good idea, it will make your code a mess.
 
Hemanth Kumar Simhadri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper, Thank you for your reply. But at the moment, already the application is UP and RUNNING. We cannot do the modification right now as it is in a JAR file. So, I am trying to hack using Eclipse. If it is possible to do something from Java programs which are not present in the JARs, I am ready to do that. Please comment
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a bug in your program. The way to fix that is to change the code, recompile, and redeploy. Surely your planning didn't assume that the program would never need any maintenance and would only be deployed once in its entire life cycle?
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like singleton pattern. i think your are fetching some comman functionalities(example : UI datas, etc..) required to an application. so that when next user login he can reuse existing object. why do you want to change it? because of the change make easier to *test*? then as jesper stated go for reflection api. i do recommend to restart server rather then jumping into reflection
 
Hemanth Kumar Simhadri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeff,
Surely there will be a maintainence. But I am looking for some temporary solution. We already planned for a permanent fix.

Thanks.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hemanth Kumar Simhadri wrote:Hello Jeff,
Surely there will be a maintainence. But I am looking for some temporary solution.


Sounds like you already have one: Restart the app.

I don't know what else you think you might be able to get a running app to do to execute different code than what you deployed, and even if you do come up with something, I have a hard time imagining it would be easier or safer than deploying a simple code change.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!