0

I am getting a warning related to one of my fields declarations, and wanted to know what would be good practice to deal with it

public class ModeloCatalogo extends Modelo {

    private static ArrayList<String> excluidas = new ArrayList<>();
    ...bunch of irrelevant fields...

    private static void functionThatLoadsValuesIntoArrayList(){
        excluidas.add("String 1");
        ...
        excluidas.add("String n");
    }
    ...bunch of irrelevant methods...
}

In the field declaration, netbeans warns "field can be final", which can be solved like:

    private static ArrayList<String> EXCLUIDAS = new ArrayList<>();

Please note that if is not uppercase, netbeans generates a different warning due to naming conventions. (added in edit for clarity)

It should be uppercase as for naming conventions which makes me cringe, doesn't seem right.

Got rid of the warning by initializing the array inside the function that loads values into array.

    private static ArrayList<String> excluidas;

    private static void functionThatLoadsValuesIntoArrayList(){
        excluidas  = new ArrayList<>();
        excluidas.add("String 1");
        ...

But it decreases code readability IMHO.

So it got me thinking, what is the correct way to get rid of the warning?

LeedMx
  • 424
  • 4
  • 19
  • Initializing static field in non static method is a code smell. – tsolakp Oct 25 '18 at 15:11
  • Upper-case notation should be used with constants and there's nothing wrong with it within context. Use a static block to populate constant mutable objects. If you're so worried about coding conventions, I'd recommend you code in English to start with. – Mena Oct 25 '18 at 15:11
  • Why don't you do what the warning says? Add the `final` keyword to the field. Since you only set it with a field initializer it doesn't have to be writable. What you did only confused the analyzer, it didn't fix the actual issue – Panagiotis Kanavos Oct 25 '18 at 15:12
  • tsolakp you're right, prone to issues will change it so the method that loads the values is static. Mena, netbeans generates warning for naming conventions, I don't like to just turn warnings off PanagiotisKanavos I did, but had to be Uppercase due to netbeans warning – LeedMx Oct 25 '18 at 16:02

1 Answers1

5

NetBeans is likely suggesting to make the field final because you only assign a value to it during initialization and don't re-assign it afterwards.

The naming convention for constants is specified in the Java Language Specification which says:

[...] and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_" characters

However, it is argued that mutable static final fields should not be considered constants and therefore the normal field naming should be used.

Newer versions of NetBeans should not show the described warning for renaming the field if the type is mutable, see this comment. Make sure the version you are using is up to date and you have not disabled this check.


But are you sure the field should be static in the first place? If the method functionThatLoadsValuesIntoArrayList can assign a value to it in your current design, then maybe the field should not be static, though this is hard to tell with this small code snippet.

Marcono1234
  • 5,856
  • 1
  • 25
  • 43
  • Static functions will need access to it, that's why it is static. I agree that I should be able to make it final and access it with a standard name. However, netbeans insist on Uppercase names for all final values. That's why i changed it. – LeedMx Oct 25 '18 at 15:59
  • According to [this comment](https://netbeans.org/bugzilla/show_bug.cgi?id=252965#c1) NetBeans should show this warning only for immutable types. Can you check that the version you are using is up to date and that you have not disabled it? – Marcono1234 Oct 25 '18 at 16:14
  • Ah thats it!!! IDE was not up to date, nice. So in the end was a "technically incorrect" warning. I knew it didn't make sense to have object fields uppercased, do you mind including that in your answer, it might help in the future to another non updated programmer. – LeedMx Oct 25 '18 at 16:23
  • Added it to the answer. Please mark the answer as accepted if you think it answered your question well enough. – Marcono1234 Oct 25 '18 at 16:57