2

I'm currently working on a project (database implementation) involving an enum class called ElementType with a inner enum called TypeType. Inside ElementType there is a HashMap<TypeType, ArrayList<ElementType>> mapping all the ElementType's to their corresponding TypeType values.

The values of TypeType are

[TEXT, NUMERIC_EXACT, NUMERIC_APPROX, OTHER]

The values of ElementType (and their corresponding TypeType) are

[CHARACTER(TEXT), CHAR(TEXT), DECIMAL(NUMBER_EXACT), DEC(NUMBER_EXACT), NUMERIC(NUMERIC_EXACT), INTEGER(NUMERIC_EXACT), INT(NUMERIC_EXACT), SMALLINT(NUMERIC_EXACT), FLAT(NUMERIC_APPROX), REAL(NUMERIC_EXACT), DOUBLE_PRECISION(NUMERIC_APPROX), DOUBLE(NUMERIC_APPROX), DATE(OTHER), TIME(OTHER), VARCHAR(OTHER), LONG_VARCHAR(OTHER)]

And in a static {} area, I have this code:

for(ElementType eType : values()) {
    TypeType t = eType.getTYPE();
    if(typeMapping.get(t) != null)
        typeMapping.get(t).add(eType);
    else
        typeMapping.put(t, new ArrayList<ElementType>() {add(eType);});
}

All of the eType mentions within the for loop are underlined red within Eclipse.

The first one gives the error eType cannot be resolved.

The second one gives eType cannot be resolved to a variable.

And the third one (inside the ArrayList) gives Syntax error on token "eType", VariableDeclaratorId expected after this token.

The getTYPE() method is private and returns the TypeType for each ElementType. The typeMapping is the HashMap mentioned above.

I don't know what is causing this or how to fix it, does anyone know how to fix this, or what I can do?

2
  • "in a static {} area" : do you mean in a static initialiser of the ElementType enum? Commented Aug 5, 2012 at 13:25
  • Next time: Please do not write a poem for something where you can show us 5 line of codes. Code which we can immediately cut&paste into your IDEs to play with. See SSCCE. Commented Aug 5, 2012 at 14:05

2 Answers 2

5

The problem is here:

typeMapping.put(t, new ArrayList<ElementType>() {add(eType);});
                                                ^^^^^^^^^^^^^  

You try to create an inner, non-static class derived from ArrayList inside a static initializer block. If you replace that with a plain new ArrayList plus alist.add(eType) it will work.

OR you can use:

typeMapping.put(t, new ArrayList<ElementType>() {{add(eType);}});

Note 1: There must be two opening and closing braces. Hence the terminus "double brace initialization".

Note 2: For DBI you must make eType final in the loop.

Although I have to admit, that the error messages are ahem somewhat unclear.

Sign up to request clarification or add additional context in comments.

2 Comments

It's also neccessary to convert eType declaration to final if second way is chosen.
Thanks, that fixed it, the first two errors i was getting i guess where just a glitch in eclipse.
4

Here's an example that compiles and works at my maching `Java HotSpot 1.6.0_21':

import java.util.*;

public enum ElementType {

    CHARACTER(TypeType.TEXT), CHAR(TypeType.TEXT), DEC(TypeType.NUMERIC_EXACT);

    public static enum TypeType {
        TEXT, NUMERIC_EXACT;
    }

    private TypeType type;

    private ElementType(TypeType type) {
        this.type = type;
    }

    private static Map<TypeType, List<ElementType>> typeMapping;

    static {
        typeMapping = new HashMap<TypeType, List<ElementType>>();
        for (ElementType eType : values()) {
            TypeType t = eType.type;
            if (!typeMapping.containsKey(t)) {
                typeMapping.put(t, new ArrayList<ElementType>());
            }
            typeMapping.get(t).add(eType);
        }

    }

    public static void main(String[] args) {
        System.out.println(typeMapping);
    }
}

Does it work for you?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.