java - 这两个构造函数上的错误空白的最终字段错误代码可能尚未初始化
问题描述
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;
}
解决方案
您必须在声明它们时为类级最终变量提供值,或者在构造函数中提供值。
如果使用第一个构造函数,最终变量errorCodes
将保持未初始化状态,这是编译器错误。
同样,如果您使用第二个构造函数,最终变量errorCode
将保持未初始化状态。
您需要为所有三个变量或 makeerrorCodes
和errorCode
non-final 提供值。
更新
而不是拥有这两个,它们做同样的事情: -
private final int errorCode;
private final String errorCodes;
你可以有:-
private final Object errorCode;
这种Object
类型将能够同时存储Integer
和String
。然后你只需要 1 个构造函数:-
DataMatchErrorCodes(Object errorCode, String errorMessage)
推荐阅读
- wordpress - 如何在订单详情页面从购买产品的客户那里获得产品评论?
- r - 加载 rsample 时遇到问题
- javascript - javascript - 关闭时的模态返回值
- html - 链接到本地托管网站中的某个位置 127.0.0.1:5500
- node.js - 您如何理解 Purescript 中的错误消息?
- php - 如果复选框不等于 wordpress 中联系表 7 中的值,则自定义验证电子邮件地址
- jenkins - bitbucket 分支源插件 - 未发生对 Bitbucket 的正在进行的构建状态通知
- craftcms - 如何从基于 PostDate 的搜索查询中排除结果?
- html - 我怎样才能让引导程序响应
- r - 从 DT::renderDT 调用时,R 闪亮的反应值不会重新计算