zzjbook
zzjbook

Reputation: 97

Android "constant expression required"

I don't want to use a duplicate flag.

package org.zzjbook.unit;
public class Unit {
    private static int flag = 1;
    public final static synchronized int getFlag() {
        return flag++;
    }
}

I use the above function to get the flag.

import static org.zzjbook.unit.Unit.getFlag;
public class Out {
    private final static int PRINT = getFlag();

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case PRINT:
                    break;
            }
        }
    }
}

IDE gives the error. "constant expression required". How can I solve this mistake.

Upvotes: 1

Views: 4864

Answers (1)

shmosel
shmosel

Reputation: 50726

switch cases need to have constant expressions, as you were told. Change it to an if instead:

public void handleMessage(Message msg) {
    if (msg.what == PRINT) {
        //...
    }
}

Upvotes: 6

Related Questions