最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】VSCode中java代码警告:The value of the field is not used

Java crifan 2947浏览 0评论
折腾:
【未解决】用Java代码解析104协议收到的数据
期间,用VSCode调试java代码期间,看到代码有警告:
src/refer/java/iec_analysis/src/main/java/com/iec/analysis/common/TypeIdentifier.java
public enum TypeIdentifier {

    private int code;
    private String hexStr;
    private String describe;

    TypeIdentifier(int code, String hexStr, String describe) {
        this.code = code;
        this.hexStr = hexStr;
        this.describe = describe;
    }
对此问题,如果像之前一样简单处理,则就是:直接删除此行
但是问题在于:
此变量在enum初始化时还要用到的
    TypeIdentifier(int code, String hexStr, String describe) {
        this.code = code;
        this.hexStr = hexStr;
        this.describe = describe;
    }
所以又不能删除
看到有 快速修复,点击试试
Remove xxx, keep assignments with side effects
试试效果:
    private int code;
    private String describe;


    TypeIdentifier(int code, String hexStr, String describe) {
        this.code = code;
        this.describe = describe;
    }
很明显,此时:
enum初始化时,变量hexStr,没有用到。
不过,去研究了下代码,还是尽量用到此处的hexStr
期间:
【已解决】Java中实现字符串格式化和拼接
去把代码改为:
    private int code;
    private String hexStr;
    private String describe;

    TypeIdentifier(int code, String hexStr, String describe) {
        this.code = code;
        this.hexStr = hexStr;
        this.describe = describe;
    }

    public static String getDescribe(int code) throws UnknownTypeIdentifierException {
        for (TypeIdentifier value : TypeIdentifier.values()) {
            if (value.code == code) {
                // String idDesc = value.hexStr + " " + value.describe;
                String idDesc = String.format("%s %s", value.hexStr, value.describe);
                return idDesc;
            }
        }

        throw new UnknownTypeIdentifierException();
    }
即可。
这样,就能利用上hexStr了。

转载请注明:在路上 » 【已解决】VSCode中java代码警告:The value of the field is not used

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.194 seconds, using 22.20MB memory