0

my goal is to avoid problem 'Private field is never assigned' without using @SupressWarnings or creating a defined constructor.

I am aware using annotation will lead to technical debt for the long run. However, I can't not justify the Java verbosity (although I love it at times when debugging a bug), this code is easier to read.

Method that I do not wish to use:

  1. SupressWarnings("unused") written above the class statement.
  2. Creating a defined constructor which is not necessary since MyBatis can modify the object attribute regardless there is a setters or not for example when you use @SelectKey.
  3. Creating a setter which will never be used.

This is the sample code for the model I am going to standardize for MyBatis.

model/NameModel.java

package com.example.mssqlserver.model;

import com.fasterxml.jackson.annotation.JsonInclude;

@SuppressWarnings("unused") // MyBatis does not need a defined constructor nor a setters.
@JsonInclude(JsonInclude.Include.NON_NULL) // filter: only non_null, alternative: spring.jackson.default-property-inclusion=NON_NULL in application.properties
public class NameModel {
  private Integer id;
  private String name;
  private String newid;

  public Integer getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public String getNewid() {
    return newid;
  }

  public boolean requestIsValid() {
    return !this.name.isEmpty();
  }
}
Jason Rich Darmawan
  • 1,607
  • 3
  • 14
  • 31
  • 1
    That's not a problem, that's a warning. You're saying you don't want to suppress the warning, and you don't want to change the code to make the warning disappear. I guess your only option is to ignore the warning with your mind then? – Kayaman Nov 17 '20 at 15:15

3 Answers3

1

the first is like this

public class MainActivity extends AppCompatActivity {
    private PDFView pdfView;

    public MainActivity(PDFView pdfView) {
        this.pdfView = pdfView;
    }
}

and I edit like this

public class MainActivity extends AppCompatActivity {
    private final PDFView pdfView;

    public MainActivity(PDFView pdfView) {
        this.pdfView = pdfView;
    }
}

and the problem solved. thanks

Ram
  • 313
  • 1
  • 2
  • 17
Jati Srono
  • 21
  • 1
1

This is normally an IDE warning, you can change the settings from the IDE to not receive this kind of warnings without tweaking your code. I don't know what IDE are you using but I think it might be IntelliJ. In that case, you can do the following:

Go to File > Settings > Editor > Inspections > Java > Declaration Redundancy > Unused declaration. And there, you can choose which elements you want to get this warning, on which privacy scope and you can even change the type of warning to a soft warning or just disable it.

In other IDEs there might be a similar option.

0

javac will not complain on this.

javac -cp *.jar com/example/mssqlserver/model/NameModel.java

So it is likely be the IDE being used. It may have a configuration Errors/Warnings option for this specific case. Unfortunately couldn't find such an option in my Eclipse IDE - though I remember there was one - so cannot point the exact option.

A related question: Why no "field is never assigned" warning with @Mock