Search code examples
javaeclipsestaticfieldfinal

java SE static final


public class Shape {
    public static int counter = 0;
}

Eclipse say to me use counter with final or use without static ? Why?


Solution

  • It's perfectly valid to have a public static that's not final, but you may have a "lint" option enabled warning you not to do it, because it's generally poor practice (with all the usual caveats that there are probably exceptions to the rule). It's impossible to say for certain as you haven't quoted the exact error/warning/message.

    Mutable public static fields make writing testing code difficult and lead to tight linking between the class with the field and the class using the fields. Again, it's perfectly valid, just usually not what you want to do.