How to code Enum type in Java example


Use the following code for using in your programs.

package enumexample;

/**
 *
 * @author tunatore
 */
//enums can be defined outside of a class
enum TSHIRT {

   XSMALL, SMALL, MEDIUM, LARGE, XLARGE, XXLARGE

}
// DO NOT REQUIRE ; at the end (optional)

public class EnumExample {

    /*
     * enums can not be private or protected
     * only public or default access is allowed
     */
    //enums defined inside a class
    enum LOGLEVEL {

        OFF, TRACE, DEBUG, INFO, WARN, ERROR, FATAL

    }

    /* enum having its own constructor */
    enum LAMP {

        RED(10, “RED Lamp”), WHITE(50), GREEN(20,”Green Lamp”), BLUE, ORANGE;

        LAMP () {
        this.watt = 15; //default value
        this.desc = “no description”;
        }

        LAMP (int watt) {
        this.watt = watt;
        this.desc = “no description”;
        }
        LAMP (int watt, String desc) {
        this.watt = watt;
        this.desc = desc;
        }
        private int watt;
        private String desc;

        public int getWatt() {
            return watt;
        }
        public String getDesc() {
            return desc;
        }

    }
  
    public static void main(String[] args) {

        LOGLEVEL level = LOGLEVEL.TRACE;
        printLevel(level);

/*     enum INSIDE_METHOD_DEFINITION_IS_NOT_ALLOWED {
 *            TEST, TEST1
 *     } wrong!!!
 *
 */
        System.out.println(“\nTSHIRT SIZES\n”);
        for (TSHIRT tshirtSize: TSHIRT.values()) {
            System.out.println(“size –> ” + tshirtSize);
        }
        System.out.println(“\nLAMPS\n”);
        for (LAMP lamp: LAMP.values()) {
            System.out.println(“watt –> ” +
                    lamp.getWatt() +
                    ” description –> ” +
                    lamp.getDesc()
                    );
        }
    }
    private static void printLevel(LOGLEVEL level)
    {
        switch (level) {
            case OFF:
                System.out.println(“OFF is selected!”);
                break;
            case TRACE:
                System.out.println(“TRACE is selected!”);
                break;
            case DEBUG:
                System.out.println(“DEBUG is selected!”);
                break;
            case INFO:
                System.out.println(“INFO is selected!”);
                break;
            case WARN:
                System.out.println(“WARN is selected!”);
                break;
            case ERROR:
                System.out.println(“ERROR is selected!”);
                break;
            case FATAL:
                System.out.println(“FATAL is selected!”);
                break;
            default:
                System.out.println(“DEFAULT”);
                break;
        }

    }
}

output of the program;

run:
TRACE is selected!

TSHIRT SIZES

size –> XSMALL
size –> SMALL
size –> MEDIUM
size –> LARGE
size –> XLARGE
size –> XXLARGE

LAMPS

watt –> 10 description –> RED Lamp
watt –> 50 description –> no description
watt –> 20 description –> Green Lamp
watt –> 15 description –> no description
watt –> 15 description –> no description
BUILD SUCCESSFUL (total time: 1 second)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s