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??

Jon Skeet
people
quotationmark

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 (e.g. 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...

people

See more on this question at Stackoverflow