BASIC / CATATAN</>↗Basic06 Agt 2009
As we know that Java enumeration type is powerful compared to enum implementation in other programming language. Basically enum is class typed, so it can have constructors, methods and fields. In the example below you’ll see how a field is defined in an enumeration type. Because each constant value for the Fruit enum is a […]
BASIC / CATATAN</>↗Basic05 Agt 2009
The valueOf() method of an enum type allows you to get an enum constant that the value corresponds to the specified string. Exception will be thrown when we pass a string that not available in the enum. package org.kodejava.basic; public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } package org.kodejava.basic; public class […]
BASIC / CATATAN</>↗Basic04 Agt 2009
To get the constants name of an enumeration you can use the values() method of the enumeration type. This method return an array that contains a list of enumeration constants. package org.kodejava.basic; public enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER } package org.kodejava.basic; public class EnumValuesTest { […]
BASIC / CATATAN</>↗Basic04 Agt 2009
In the following example you’ll see how to add a constructor to an enum type value. Because an enum is just another class type it can have constructors, fields and methods just like any other classes. Below we define a constructor that accept a string value of color code. Because our enum now have a […]
BASIC / CATATAN</>↗Basic04 Agt 2009
This example show you how to use enumeration or enum type in a switch statement. package org.kodejava.basic; public enum RainbowColor { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET } package org.kodejava.basic; public class EnumSwitch { public static void main(String[] args) { RainbowColor color = RainbowColor.INDIGO; EnumSwitch es = new EnumSwitch(); String colorCode = es.getColorCode(color); System.out.println("ColorCode […]
BASIC / CATATAN</>↗Basic01 Agt 2009
Enumeration is a list of named constants. Every most commonly used programming languages support this feature. But in Java it is officially supported since version 5.0. In Java programming language an enumeration defines a class type. Because an enumeration is a class it can have a constructors, methods, and instance variables. To create an enumeration […]