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

  • Home
  • SEARCH
  • 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 3970022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:02:11+00:00 2026-05-20T04:02:11+00:00

I am a little confused here with this findbugs warning in eclipse. public class

  • 0

I am a little confused here with this findbugs warning in eclipse.

public class MyClass {
    public static String myString;
}


public class AnotherClass {
   public void doSomething() {
       MyClass.myString = "something";
   }
}

This gives me a findbugs warning “write to static field from instance method”, however this does not give me a warning:

public class MyClass {
    public static String myString;
}


public class AnotherClass {
   public void doSomething() {
       doAnotherThing();
   }
   public static doAnotherThing() {
       MyClass.myString = "something";
   }
}

How is this any different?, and why is writing to a static variable from an instance method a bad practice?, I assume it has to do with synchronization, but it is still not clear to me.

I know this looks like the variable should be final, but I am loading the value from a properties file.

  • 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-05-20T04:02:12+00:00Added an answer on May 20, 2026 at 4:02 am

    Its a form of aliasing, which may be counter-intuitive. Counter-intuitive code hampers ease of maintenance.

    Logically, we expect instance methods to affect that instance’s data. We expect static methods to affect static data.

    Let’s rename doSomething to initialize:

    ...
    a.initialize();
    ...
    b.initialize();
    ...
    

    The reader of this code may not immediately realize that the instances of a and b are actually affecting the same data. This may be a bug since we’re initializing the same memory twice, but its non-obvious since it seems reasonable that we may need to call initialize on each instance.

    However, the the code were:

    ...
    MyClass.initialize();
    ...
    MyClass.initialize();
    ...
    

    In this case, its more intuitive that we’re likely affecting the same static data and this is likely a bug.

    This is similar to the common version of aliasing where two variables in the same scope point to the same instance.


    For your last example,

    • an instance calls a static method

      The fact that an instance method is calling a static method isn’t expected to raise flags. The examples were this is useful far outweigh where its likely a problem.

    • a static method of one class affects another class’ static data

      In one sense, it should generate a different, but similar warning: that one class is messing with the data of another class. However, by making the static variable public is a way of tacitly approving of this, so such a warning isn’t necessary.

    Keep in mind that FindBugs is simply trying to flag potential likely problems, not every possible problem, in your code. Your first example is likely a potential maintenance issue that you need to examine whether its a real problem. Your second example is likely not a problem or it is a real problem that is too similar to use cases where it is not a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little confused here. I would like to do something like this:
I'm a little confused by some PHP syntax I've come across. Here is an
I'm a little confused about how the standard library will behave now that Python
I am a little confused by the multitude of ways in which you can
I am a little confused as to the definition of classes as Models or
Im a little confused over how to use the .NET Trace and Debug classes.
I am getting a little confused and need some help please. Take these two
When supplying dates to a stored procedure via a parameter I'm a little confused
I'm playing around with Project Euler's Problem 220 , and I'm a little confused
My little site should be pooling list of items from a table using the

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.