A Practical Example of Enum inwards Java

Enum is a useful characteristic of Java together with the best agency to stand upwardly for fixed release of things e.g. a release of the planet inwards the solar system, the release of terra firma companionship tin move or merely a release of days inwards a week. Java Enum is dissimilar than their predecessor on other languages similar C together with C++, where the enum is exactly a wrapper around integer constant. Enum inwards Java is a sum blown type similar course of written report together with interface, they tin conduct keep constructor (though alone mortal constructors are permitted for Enum), they tin override methods, they tin conduct keep fellow member variables, they tin fifty-fifty declare methods, tin implement interfaces together with conduct keep specialized high functioning Map together with Set implementation inwards shape of EnumMap together with EnumSet.

I conduct keep written a lot of articles inwards Enum, exploring it's dissimilar lesser known features together with involving some best practices, but several times I have a asking for simple Enum examples, together with that's why, I am sharing this, 1 of the most uncomplicated examples of Enum inwards Java. In this Enum example, I conduct keep used Enum to stand upwardly for SoftDrink inwards a convenience store. This Enum course of written report has iv types of soft drinks Coke, Pepsi, Soda, together with Lime


To demonstrate Enum tin conduct keep constructor together with fellow member variables, I conduct keep provided them title together with price, together with to demonstrate Enum tin override methods, I conduct keep overridden toString() method, which returns custom championship passed to each SoftDrink. Apart from the custom title, y'all tin every bit good avail implicit name() method of Enum to larn a String representation of Enum constant, it returns exact String literal used to declare Enum constant, for example, SoftDrink.COKE.name() volition render COKE piece SoftDrink.COKE.toString() volition render Coke here. 



Here is some of import points nearly Enum inwards Java, which is worth remembering every bit well:


Enum is a useful characteristic of Java together with the best agency to stand upwardly for fixed release of things e H5N1 Practical Example of Enum inwards Java


Let's run across the instance outset together with afterward I volition explicate some fundamental things nearly Enum inwards Java.


Simple Java Enum Code Example - SoftDrink

import java.util.EnumMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory;  /**  * Java computer program to present How to purpose Enum inwards Java via a Simple Example  *  * @author Javin Paul  */ public class JavaEnumExample {      private static final Logger logger = LoggerFactory.getLogger(JavaEnumExample.class);          public static void main(String args[]) {                // EnumMap is a exceptional high functioning map to shop Enum constrants         Map<SoftDrink, Integer> shop = new EnumMap<SoftDrink, Integer>(SoftDrink.class);                 // Let's initialize store, past times storing 10 canes of each drink         // Enum provides an implicit values() method, which tin move used to iterate over Enum         for(SoftDrink quaff : SoftDrink.values()){             store.put(drink, 10);         }                 // let's impress what is inwards EnumStore              for(Map.Entry<SoftDrink, Integer> entry: store.entrySet()){             logger.debug(entry.getKey() + " Qty: " + entry.getValue() 
                  + " Price: " + entry.getKey().getPrice());         }     }           }  
public enum SoftDrink{     COKE("Coke", 75), PEPSI("Pepsi", 75), SODA("Soda", 90), LIME("Lime", 50);         // Java Enum tin conduct keep fellow member variables     private String title;     private int price; // inwards cents         // You tin declare constructor for Enum inwards Java     private SoftDrink(String title, int price){         this.title = title;         this.price = price;     }         // Enum tin conduct keep methods inwards Java        public String getTitle(){         return title;     }         public int getPrice(){         return price;     }      // Enum tin override methods inwards Java     @Override     public String toString() {         return title;     }      }  Output 2013-07-03 05:39:42,878 0    [main] DEBUG JavaEnumExample  - Coke Qty: 10 Price: 75 2013-07-03 05:39:42,878 0    [main] DEBUG JavaEnumExample  - Pepsi Qty: 10 Price: 75 2013-07-03 05:39:42,878 0    [main] DEBUG JavaEnumExample  - Soda Qty: 10 Price: xc 2013-07-03 05:39:42,878 0    [main] DEBUG JavaEnumExample  - Lime Qty: 10 Price: 50
Enum tin move compared using == operator every bit good every bit equals() method, it gives a selection to purpose erstwhile for amend performance. 

Every Enum inwards Java implicitly implements java.lang.Enum (compiler does that for you), which provides some convenient methods similar the name(), values() together with valueOf(), hither nosotros conduct keep used values() together with a for each loop to iterate over all Enum constant, quite a bully idiom post-Java 5.

That's all on this Simple Java Enum Example. It's plenty to larn y'all started alongside Enum together with every bit good demonstrate some of the fundamental features of Java enum. I advise looking my some other article 10 Java Enum Examples together with next mass JavaDeveloper's Notebook for to a greater extent than information on How to conduct keep most of Enum inwards Java.


Other Java Enum Tutorials from Blog

Subscribe to receive free email updates:

0 Response to "A Practical Example of Enum inwards Java"

Posting Komentar