简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM