简体   繁体   中英

How to correctly use constructors?

Hi I am new to Java and constructors are still consfusing me. I am trying to create and enum as below:

public enum SCardErrors
{
    SCARD_S_SUCCESS(0x0L),
    SCARD_E_SHARING_VIOLATION(0x8010000BL),
    SCARD_E_NO_READERS_AVAILABLE(0x8010002EL),
    SCARD_E_READER_UNAVAILABLE(0x80100017L);

    private int intValue;
    private static java.util.HashMap<Integer, SCardErrors> mappings;
    private static java.util.HashMap<Integer, SCardErrors> getMappings()
    {
        if (mappings == null)
        {
            synchronized (SCardErrors.class)
            {
                if (mappings == null)
                {
                    mappings = new java.util.HashMap<Integer, SCardErrors>();
                }
            }
        }
        return mappings;
    }

    private SCardErrors(int value)
    {
        intValue = value;
        getMappings().put(value, this);
    }

    public int getValue()
    {
        return intValue;
    }

    public static SCardErrors forValue(int value)
    {
        return getMappings().get(value);
    }
}

From above it gives the error that constructor SCardErrors(long) is undefined for the lines: SCARD_S_SUCCESS(0x0L), SCARD_E_SHARING_VIOLATION(0x8010000BL), SCARD_E_NO_READERS_AVAILABLE(0x8010002EL), SCARD_E_READER_UNAVAILABLE(0x80100017L);

I have tried adding in SCARD_S_SUCCESS(0x0L){ } for each of them but it did not fix the error. I also tried adding them as a parameter but this didn't work. Can someone please help??

You've only got a constructor accepting int :

private SCardErrors(int value)

... but you're trying to call it with an argument of type long :

SCARD_S_SUCCESS(0x0L)

You either need to change the constructor to accept a long - and probably change the field type as well - or change the arguments to be int values instead. Not all of your arguments can be represented as int (eg 0x80100017L) - but if you only care about the bits involved, and don't mind the values becoming negative, you could cast to int and still have the same 32 bits...

The reason Java does not find the constructor is that you used L suffix on all your numeric literals, making them long , not int . Java does not allow conversion of long to int without an explicit cast, so the compiler cannot apply the constructor that takes an int in this situation (the other way around, ie passing an int to a method or a constructor expecting a long , would have worked).

You can fix this by changing the compiler to take long , and using longValue in place of intValue :

private long longValue;
private static java.util.HashMap<Long, SCardErrors> mappings;
private static java.util.HashMap<Long, SCardErrors> getMappings()
{
    if (mappings == null)
    {
        synchronized (SCardErrors.class)
        {
            if (mappings == null)
            {
                mappings = new java.util.HashMap<Long, SCardErrors>();
            }
        }
    }
    return mappings;
}

private SCardErrors(long value)
{
    longValue = value;
    getMappings().put(value, this);
}

public long getValue()
{
    return longValue;
}

public static SCardErrors forValue(long value)
{
    return getMappings().get(value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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