switch case con enum code example

Example 1: java use external enum in another class switch statement

Determine the enum value represented by the int value and then switch on the enum value.

enum GuideView {
    SEVEN_DAY,
    NOW_SHOWING,
    ALL_TIMESLOTS
}

// Working on the assumption that your int value is 
// the ordinal value of the items in your enum
public void onClick(DialogInterface dialog, int which) {
    // do your own bounds checking
    GuideView whichView = GuideView.values()[which];
    switch (whichView) {
        case SEVEN_DAY:
            ...
            break;
        case NOW_SHOWING:
            ...
            break;
    }
}

Example 2: java switch case enum

switch (BD.usuarioLogado.getTipoAcesso()) {
            case ALUNO:
                Aluno alunoLogado = (Aluno) BD.getUsuarioLogado().clone();
                jTextFieldNOME.setText(alunoLogado.getUsername());
                jTextFieldNOME.setEditable(false);

                jTextFieldDATANASCIMENTO.setText(alunoLogado.getDataNascimento());
                jTextFieldDATANASCIMENTO.setEditable(false);
                
                break;
            case PROFESSOR:
                Professor usuarioLogado = (Professor) BD.getUsuarioLogado().clone();
                break;
            default:
                break;
        }

Tags:

Java Example