简体   繁体   English

Java枚举常量值不会改变

[英]Java enum constant value won't change

So this isn't one of those "I want to change a constant value during runtime, why is it not working?" 因此,这不是“我想在运行时更改常数值,为什么它不起作用?”之一。 questions, just as a heads-up. 问题,就像单挑一样。 I'm having trouble changing a Java Enum's value in code. 我在更改代码中的Java枚举值时遇到麻烦。 I've made the change, saved, and re-run my program, but the value in-code has remained with the old value. 我进行了更改,保存并重新运行了程序,但是代码中的值仍与旧值保持一致。 I'm using the Enum.values() method to cycle through my Enum constants and the value is showing as the old value, not the new value I've directly typed into the enum. 我正在使用Enum.values()方法循环遍历我的Enum常数,并且该值显示为旧值,而不是我直接键入枚举的新值。 I'm starting to wonder if I'm losing my mind or my program has decided not to recompile my Enum or what. 我开始怀疑我是否正在失去理智,或者我的程序决定不重新编译我的Enum或什么。 A sanity check would be welcome. 欢迎进行健康检查。 See below for the entirety of my enum's code and where I am accessing the values. 请参阅下面的完整枚举代码以及在其中访问值的位置。

public enum MSSQLType implements DBType {
//String Types
CHAR(8000, -1, null, "CHAR","TEXT"),
VARCHAR(8000, -1, null, "VARCHAR", "TEXT"),
TEXT(-1, -1, null, "TEXT", "TEXT"),
NCHAR(4000, -1, null, "CHAR", "TEXT"),
NVARCHAR(4000, -1, null, "VARCHAR", "TEXT"),
NTEXT(-1, -1, null, "TEXT", "TEXT"),
//Numeric Types
BIGINT(19, -1, null, "BIGINT", "BIGINT"),
BIGINT_IDENTITY (19, -1, null, null, null),
INT(10, -1, null, "INT", "BIGINT"),
SMALLINT(5, -1, null, "SMALLINT", "BIGINT"),
TINYINT(3, -1, null, "TINYINT", "BIGINT"),
BIT(1, -1, null, "TINYINT", "BIGINT"),
DECIMAL(14, 38, null, "DECIMAL", "DECIMAL"),
NUMERIC(14, 38, null, "DECIMAL", "DECIMAL"),
MONEY(15, 4, null, "DECIMAL", "DECIMAL"),
SMALLMONEY(6,4, null, "DECIMAL", "DECIMAL"),
FLOAT(-1, 53, null, "FLOAT", "FLOAT"),
REAL(-1, 106, null, "FLOAT", "FLOAT"),
//Date/time types
DATETIME(-1, -1, "YYYY-mm-DD HH:MM:SS", "DATETIME", "DATETIME"),
SMALLDATETIME(-1, -1, "YYYY-mm-DD HH:MM:SS", "DATETIME", "DATETIME"),
//Other types
BINARY(-1,-1, null, null, null),
VARBINARY(-1,-1, null, null, null),
IMAGE(-1,-1, null, null, null),
CURSOR(-1,-1, null, null, null),
SQL_VARIANT(-1,-1, null, null, null),
TABLE(-1,-1, null, null, null),
TIMESTAMP(-1, -1, "YYYY-mm-DD HH:MM:SS", "DATETIME", "DATETIME"),
UNIQUEIDENTIFIER(-1, -1, null, null, null);

private int size;
private int precision;
private String format;
private String convertTo;
private String fallback;

MSSQLType(int size, int precision, String format, String convertTo, String fallback) {
    this.size = size;
    this.precision = precision;
    this.format = format;
    this.convertTo = convertTo;
    this.fallback = fallback;
}
public int getSize() {
    return size;
}

public int getPrecision() {
    return precision;
}

public String getFormat() {
    return format;
}

public String getConvertTo() {
    return convertTo;
}

public String getFallback() {
    return fallback;
}

@Override
public String getDisplay() {
    return toString();
}

@Override
public String toString() {
    return super.toString().replaceAll("_", " ");
}
}  

So the convertTo and fallback values of REAL were changed to "FLOAT", "FLOAT" from "DOUBLE", "DOUBLE", but the code is still assigning the constant String "DOUBLE" to those two values! 因此,REAL的convertTo和fallback值从“ DOUBLE”,“ DOUBLE”更改为“ FLOAT”,“ FLOAT”,但是代码仍将常量字符串“ DOUBLE”分配给这两个值! The values are being read out as so: 值将按以下方式读取:

for (MSSQLType msSqlType : MSSQLType.values()) {
            if (typeName.equalsIgnoreCase(msSqlType.toString())) {
                this.type = msSqlType;
                return;
            }
        }

I've also tried using MSSQLType.class.getEnumConstants() with the same result. 我也尝试过使用MSSQLType.class.getEnumConstants()获得相同的结果。

Any help appreciated. 任何帮助表示赞赏。

You are using msSqlType.toString() method you should use msSqlType.name() method. 您正在使用msSqlType.toString()方法,而应使用msSqlType.name()方法。 Default toString() method on enum returns name but not if you have overridden it in DBType class. enum上的默认toString()方法返回name但如果您在DBType类中重写了该方法,则不会返回name

 if (typeName.equalsIgnoreCase(msSqlType.name())) {
            this.type = msSqlType;
            return;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM