首页 > 解决方案 > 这两个构造函数上的错误空白的最终字段错误代码可能尚未初始化

问题描述

public enum DataMatchErrorCodes {

  PAYLOAD_IS_EMPTY(100, "payload is empty or invalid payer"),
  MULTIPLE_PROVIDERFOUND("zz", "Multiple Provider Found"),
  PROVIDER_NOTFOUND(43, "provider not found"),
  PROCESS_MSG_ERROR(53, "unable to process msg");


    private final int errorCode;
    private final String errorMessage;
    private final String errorCodes;



    DataMatchErrorCodes(int errorCode, String errorMessage) {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
      }
    DataMatchErrorCodes( String errorCodes, String errorMessage) {
        this.errorCodes = errorCodes;
        this.errorMessage = errorMessage;
      }

标签: java

解决方案


您必须在声明它们时为类级最终变量提供值,或者在构造函数中提供值。

如果使用第一个构造函数,最终变量errorCodes将保持未初始化状态,这是编译器错误。

同样,如果您使用第二个构造函数,最终变量errorCode将保持未初始化状态。

您需要为所有三个变量或 makeerrorCodeserrorCodenon-final 提供值。


更新

而不是拥有这两个,它们做同样的事情: -

private final int errorCode;
private final String errorCodes;

你可以有:-

private final Object errorCode;

这种Object类型将能够同时存储IntegerString。然后你只需要 1 个构造函数:-

DataMatchErrorCodes(Object errorCode, String errorMessage)

推荐阅读