Tuesday, September 13, 2011

How to use SWITCH statement for Strings (with the help of ENUM)


package com.practice;

public class StringInSwitchTest {

    private enum PERSON_NAME {
        JOHN, ROBERT, MATTHEW
    }

    private void stringTest(String person) {
        switch (PERSON_NAME.valueOf(person)) {
            case JOHN:
                System.out.println("The person is JOHN");
                break;

            case ROBERT:
                System.out.println("The person is ROBERT");
                break;

            default:
                System.out.println("The person is MATTHEW");
        }
    }

}

No comments:

Post a Comment