• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unsafe assignment to non-final static field

 
Sean Riley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In an attempt to begin coding with some sort of consistant structure I looked at PMD which basically checks any code and offers hints based on a ruleset.

Consider the following code...



I need to check for the existance of "MyEnvVar" which is an environment variable and put whatever is in that environment variable in the String variable - sMyEnvVar. If the var is not set I will throw an exception etc.

The code works, however PMD complains about "Possible unsafe assignment to a non-final static field in a constructor." I am not concerned about PMD being any sort of holy grail to fix all my coding faults but it is a starting point.

1) What does the 'unsafe assignment' message mean?
2) How can the above code be written more correctly?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just guessing ... this is "possibly unsafe" because it is not thread safe. Two threads reading and writing the same static variable could wind up in a race condition,.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your code compile or did you just introduce a typo when posting? I ask because the static variable does not have a type.

Layne
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've declared sMyEnvVar as final..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message suggests several solutions: "Possible unsafe assignment to a non-final static field in a constructor."

It complains because it's static ... make it not static.

It complains because it's not final ... make it final.

It complains because it's set in a constructor ... set it elsewhere.

Or, if it's only a warning, live with it.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code updates a class variable every time an instance of that class is created. That sounds a little shakey but let's assume that's actually what you want. In that case synchronize access to sMyEnvVar.



BTW, what's the 's' prefix for on sMyEnvVar?
[ October 01, 2005: Message edited by: Rick O'Shay ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent. Add to my list: the compiler complains because it's unsafe ... make it safe!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic