This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Unsafe assignment to non-final static field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unsafe assignment to non-final static field" Watch "Unsafe assignment to non-final static field" New topic
Author

Unsafe assignment to non-final static field

Sean Riley
Greenhorn

Joined: Jun 08, 2004
Posts: 11
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?


Of all the things I've lost in my life it's my mind I miss the most.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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,.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
You've declared sMyEnvVar as final..


Regards,<br />Seb<br /> <br />SCJP 1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Sep 19, 2004
Posts: 531
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

Joined: Jan 29, 2003
Posts: 8791
Excellent. Add to my list: the compiler complains because it's unsafe ... make it safe!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unsafe assignment to non-final static field
 
Similar Threads
static init block in interfaces
NullPointerException
PMD - Customize ShortVariable rule
In interface if any variable is declared then no need to initialize.
Sample Questions from Sun