Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:22:54+00:00 2026-06-15T01:22:54+00:00

I have a Java class like this: public class Foo { public static int

  • 0

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, am I not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:22:55+00:00Added an answer on June 15, 2026 at 1:22 am

    Suppose that at some point in the future, you decide this setter method needs to be thread safe and you want to make it synchronized.

    This code will work fine:

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

    This code is wrong and will have incorrect behavior:

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

    This might not seem like a significant difference in this contrived example, especially since counter can usually just be marked volatile. However, in a real world example where the setter method has more complicated logic and is being called from many different places (not just from one instance method), the latter pattern will be easier to refactor.

    As an aside, in my opinion Google’s CodePro Analytix plugin is a much faster and more comprehensive tool than FindBugs.

    Related:

    • Synchronized vs. Volatile in Java
    • Synchronized Getters and Setters
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Java class like this: public class Test { static {
I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val)
Lets say I have a class like this in Java: public class Function {
I have a Java class which is something like the following: public class Foo{
So I have a class of overloaded methods like this: class Foo { public
I have a .java src file that looks like this: class Test { public
Say I have classes Foo and Bar set up like this: class Foo {
In Java I have an object that looks like this : class MyDoc {
I want to have immutable Java objects like this (strongly simplified): class Immutable {
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.