8

I have couple of areas in my application where I get the error while manipulating value of static variable from instance method.

"Write to static field from instance method".

If we take multi-threading out of the equation, does this scenario pose any potential issue even if multiple instances write to the same static variable ?

Not a bug
  • 4,286
  • 2
  • 40
  • 80
Baz
  • 113
  • 1
  • 1
  • 8
  • check this SO question http://stackoverflow.com/q/3630485/1686291. – Not a bug Jan 15 '14 at 11:41
  • I specifically want to know when threading is not involved, if this scenario will likely create any problem..Some explanation would be nice. – Baz Jan 15 '14 at 11:45

1 Answers1

21

From the documentation...

This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice.

  • Firstly it says that it is a bad practice, not incorrect.
  • Second thing is the question, about posing any potential issue

    If you are manipulating a static field from instance method, any object of class( class that contains our instance method) may be calling that method and it will be difficult to find out the object manipulating static field in some large application or application that is already developed and coded by others.

This Answer may help you also.

EDIT :

FYI, You can bypass the warning of findbug in following code.

class TestClass {

     static int testInt = 0 ;

     public static setTestInt ( int a ) {
          TestClass.testInt = a ;
     }

     public void setInt ( int a1 ) {
          setTestInt ( a1 ); 
     }
}
Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
  • So, I guess in a single threaded environment, this should not be an issue,right? – Baz Jan 15 '14 at 12:02
  • No, sometimes that can be. i think you didn't get last paragraph properly ? – Not a bug Jan 15 '14 at 12:03
  • What I meant is there any potential issue to the integrity of the data referred by the static variable if it is single threaded? And thanks by the way for your prompt response. – Baz Jan 15 '14 at 12:15
  • Thats what I was looking for...Now I`m just trying to understand how the data integrity will be compromised. For eg. In a single threaded scenario, if multiple instances are acting on this piece of code,there is no possibility of simultaneous execution,rt?(because of no threading). So each instance , one after the other will act on the code and finally the variable will have whatever value that was set by the last instance. So whatever value of static field an instance started working with, it wont be changed in between as there is no parallel execution. So how is data integrity affected here? – Baz Jan 15 '14 at 12:54
  • It is just a bad practice for single threaded env. because u will always be able to find the cause after the changed value. but sometimes that can be difficult :p – Not a bug Jan 15 '14 at 12:58
  • 2
    Appreciate your effort in responding. – Baz Jan 15 '14 at 12:59