Total members 11345 |It is currently Fri Aug 01, 2014 4:42 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

All times are UTC [ DST ]



  Java enum example
Fri Mar 20, 2009 8:17 pm 

Java Enum Example :

Enum define a set of constant variables. Please read the following hints about Java Enums :
  • To define Enum type you have to use enum keyword.
  • All enums extend java.lang.Enum.
  • You can define Enum inside class as any class member.
  • Enum can't extends (inherit ) any other type.
  • Enum type constructor must be package-private.
  • Enum declaration defines a class (called an enum type).
  • Enum class can include methods and other variables.
  • Compiler automatically adds some methods when it compile an Enum such as values() and valueOf() functions.


Please read the following examples about Java ENUMS type.

Enum Type Declaration


java code
enum LevelsType{
level1,level2,level3,level4
}
 



The following example defines a string Enums values and use switch expression to control decision based on Enum values :
java code
public class Main {

public static void main(String[] args) {

PaymentTypes currentPayType=PaymentTypes.NEW;

switch(currentPayType)
{
case PREPAID:
System.out.println("This type is prepaid");
break;
case POSTPAID:
System.out.println("This type is postpaid");
break;
case NEW:
System.out.println("This type is new");
break;
default:
System.out.println("This type is not defined!!");
break;
}

}
}

enum PaymentTypes {
PREPAID, POSTPAID, NEW
}


The output of the above ENUM  example
java code
This type is new


Using CompareTo to compare two Enum Values


This is case we use CompareTo auto generated function to compare two Enums values of the same Enum type(The must be of the same type). CompareTo() returns 0 (If two are equal), -1 ( If second parameter is less ordered) or 1 ( highly ordered ) in the Enum Type definition.
java code
public class Main {

public static void main(String[] args) {

PaymentTypes newpayType=PaymentTypes.NEW;
PaymentTypes prepaidType= PaymentTypes.PREPAID;

if(newpayType.compareTo(prepaidType)==0)
{
System.out.println("The two enum values are equal and of same order");
}else if(newpayType.compareTo(prepaidType)==-1)
{
System.out.println("The two enum values are different and parameter is highly ordered");

}else
{
System.out.println("The two enum values are different and parameter is less ordered");
}
}
}

enum PaymentTypes {
PREPAID, POSTPAID, NEW
}

The output is :
Code:
The two enum values are different and parameter  is less ordered

The order here is the order in which the enum values are declared.


Enum Type Constructor


Note that the Enum's constructor has to be private , and you can't access it , this private constructor defines the constants listed in the beginning of the Enum body:
java code
public enum CarEnum {
 
    BMW("BMW"), TOYOTA("TOYOTA"), FIAT("FIAT");
    private String CarType;
 
    private CarEnum(String CarType) {
    this.CarType = CarType;
    }

     public String getCarType() {
    return CarType;
    }
}
 


you may need to know how to access a value of Enum, that is why I added this example of accessing Enum in Java. For example the following code snippet shows you how to access car Enum in the main class:

java code
public class EnumTest {
static CarEnum mycar;
    public static void main(String args[])
    {
        System.out.println(mycar.BMW.getCarType());
    }
}
 


To print all values of a specific Enum Type


For each ENUM class type a values functions is auto generated which can be used to print all the ENUMs available.
java code
public class Main {

public static void main(String[] args) {
// To print all the values of enum type :
for (CarEnum carType : CarEnum.values()) {
System.out.println("Car type:- " + carType);
}
}
}
enum CarEnum {
BMW("BMW"), TOYOTA("TOYOTA"), FIAT("FIAT");
private String CarType;
private CarEnum(String CarType) {
this.CarType = CarType;
}
public String getCarType() {
return CarType;
}
}

Using valueOf function to return Enum


Return enum constant type if a specific declared Enum constant identifier matches the string passed to valueOf() function.
java code
public class Main {
public static void main(String[] args) {

System.out.print(CarEnum.valueOf("BMW"));
}
}
enum CarEnum {
BMW("Bavarian Motor Works"),
TOYOTA("Too Often Yankees Overprice This Auto"),
FIAT("Fabbrica Italiana Automobili Torino");
private String CarType;
private CarEnum(String CarType) {
this.CarType = CarType;
}
public String getCarType() {
return CarType;
}
}

The output of this program is :
Code:
BMW


Define Enum Type As Inner Variable (Inside a Class)


Note that Enums can't be extended . But you can define Enums inside a class like any member :
java code
public class Main {

public static void main(String[] args) {

System.out.println(new BillingAccount());
// You can access the enum as class member:
// If private access you can acces it directly as follows:
System.out.println(BillingAccount.PaymentTypes.NEW);

}
}

class BillingAccount {

enum PaymentTypes {

PREPAID, POSTPAID, NEW
}
private String billingNumber;
private String paymentType;

public BillingAccount() {
billingNumber = "32423";// Any Data
paymentType = PaymentTypes.POSTPAID.toString();
}

@Override
public String toString() {
return "paymentType= " + paymentType;

}
}


The output is :
java code
paymentType= POSTPAID
NEW

Another Example :

java code
public class EnumTest {
     enum Food {
    BREAD("BREAD"),CHEESE("CHEESE"),MEAT("MEAT");
    private String myfood;
    Food(String s)
    {
      myfood = s ;
    }
  public String getValue()
  {
      
      return myfood ;
  }
     }

 
    public static void main(String args[])
    {
         System.out.println(Food.BREAD.myfood+"  "+Food.BREAD+" "+Food.values());
         new EnumTest2();
    }
}
class EnumTest2 {

    EnumTest2()
    {
        System.out.println(EnumTest.Food.BREAD);
    }
}
 


As you see the difference , you can access the Enum directly inside a static function .

The output is :
Code:
BREAD  BREAD [LEnumTest$Food;@11b86e7
BREAD


Passing Enum as Type Parameter method


Following example I print a Enum parameter value sent to a static method.
java code
public class Main {

public static void printEnum(CarEnum carEnum)
{
System.out.println(carEnum);
}
public static void main(String[] args) {
printEnum(CarEnum.FIAT);
}
}
enum CarEnum {
BMW("BMW"), TOYOTA("TOYOTA"), FIAT("FIAT");
private String CarType;
private CarEnum(String CarType) {
this.CarType = CarType;
}
public String getCarType() {
return CarType;
}
}


The output is :
Code:
FIAT


Extending enum in java. Can we extend (inherit) an enum type?


Extending Enums in java is not allowed. As example suppose you have two Enums A and B. You can't do the following :

java code
enum B
{
}
enum A extends enum B
{
   // Define you enum here .



And in the same time you can't use implements keyword , because B is not an interface ,But if your system need both to be same type. For example, if you want to define one function signature to take both Enums as parameters you can do it by defining an interface and implementing it in both Enums .

java code
interface enuminterface
enum B implements enuminterface
{
}
enum A  implements enuminterface

{
   // Define you enum here .


and your signature will be something like this :
java code
 return type myfunction (enuminterface Enumparam)  ;  


_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time
  Re: Java enum example
Thu Jun 20, 2013 12:44 am 
updated.

_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Java enum example
 Define enum in java     -  
 extending enum in java     -  
 Enum Declaration     -  
 how to use enum in jaxb unmarshalling     -  
 Define Enum inside a class     -  
 Passing Enum as Type Parameter to method     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Java course     -  
 What is Java API?!!!     -  

All times are UTC [ DST ]



Topic Tags

Java OOP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team