Saturday, July 4, 2015

Java Enumerations

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST and WEST) and the days of week. 

for e.g. public enum Day 
{
SUNDAY,
MONDAY,
TUESDAY.
WEDNESDAY.
THURSDAY.
FRIDAY,
SATURDAY
}

Java programming language enum types are more powerful than any other counter part languages. The enum declaration defines a class (called an enum type) The enum class body can include methods and other variables! . Example is like below 

public enum Planet
{
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7);

private final double Mass;
private final double Radius;

Planet (double mass, double radius)
{
this.mass = mass;
this.radius = radius;
}

public double getMass()
{
return this.mass;
}
}

references:

No comments:

Post a Comment