Write to static field - is FindBugs wrong in this case?

Posted by htorque on Stack Overflow See other posts from Stack Overflow or by htorque
Published on 2012-11-14T22:57:35Z Indexed on 2012/11/14 22:59 UTC
Read the original article Hit count: 324

I have a Java class like this:

public class Foo {

    public static int counter = 0;

    public void bar(int counter) {
        Foo.counter = counter;
    }
}

FindBugs warns me about writing to the static field counter via the instance method bar. However, if I change the code to:

public class Foo {

    public static int counter = 0;

    public static void setCounter(int counter) {
        Foo.counter = counter;
    }

    public void bar(int counter) {
        setCounter(counter);
    }
}

Then FindBugs won't complain. Isn't that wrong? I'm still writing to a static field from an instance method, just via a static method - no?

© Stack Overflow or respective owner

Related posts about java

Related posts about static-methods