Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

Java Enum example

Revision 1 vs. Revision 2

Legend:

Unmodified
Added
Removed
  • Code

    r1 r2  
    1 //package com.wickedcooljava.core;  
    2   
    3 /*  
    4 Example code from Wicked Cool Java (No Starch Press)  
    5 Copyright (C) 2005 Brian D. Eubanks  
    6   
    7 This library is free software; you can redistribute it and/or  
    8 modify it under the terms of the GNU Lesser General Public  
    9 License as published by the Free Software Foundation; either  
    10 version 2.1 of the License, or (at your option) any later version.  
    11   
    12 This library is distributed in the hope that it will be useful,  
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of  
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
    15 Lesser General Public License for more details.  
    16   
    17 You should have received a copy of the GNU Lesser General Public  
    18 License along with this library; if not, write to the Free Software  
    19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  
    20   
    21 Note: The LGPL licence can be found online at http://www.gnu.org  
    22   
    23 */  
    24   
    25 /**  
    26  * This example shows the use of enum types in Java 5.0  
    27  */  
    28 public class FruitSalad {  
    29   
    30     public enum FruitCategory {  
    31         SWEET, CITRUS, SMELLY  
    32     }  
    33   
    34     public enum Fruit {  
    35         APPLE {  
    36             FruitCategory getCategory() {  
    37                 return FruitCategory.SWEET;  
    38             }  
    39         },  
    40         ORANGE {  
    41             FruitCategory getCategory() {  
    42                 return FruitCategory.CITRUS;  
    43             }  
    44         },  
    45         GRAPEFRUIT {  
    46             FruitCategory getCategory() {  
    47                 return FruitCategory.CITRUS;  
    48             }  
    49         },  
    50         BANANA {  
    51             FruitCategory getCategory() {  
    52                 return FruitCategory.SWEET;  
    53             }  
    54         },  
    55         DURIAN {  
    56             FruitCategory getCategory() {  
    57                 return FruitCategory.SMELLY;  
    58             }  
    59         };  
    60   
    61         abstract FruitCategory getCategory();  
    62     }  
    63   
    64     public static void main(String[] args) {  
    65         Fruit a = Fruit.APPLE;  
    66         System.out.println("The toString() for a: " + a);  
    67         System.out.println("a.getCategory() is: " + a.getCategory());  
    68   
    69         for (Fruit f : Fruit.values()) {  
    70             System.out.println("Fruit is: " + f);  
    71         }  
    72     }  
    73 }  
     1//package com.wickedcooljava.core; 
    742 
     3/* 
     4Example code from Wicked Cool Java (No Starch Press) 
     5Copyright (C) 2005 Brian D. Eubanks 
     6 
     7This library is free software; you can redistribute it and/or 
     8modify it under the terms of the GNU Lesser General Public 
     9License as published by the Free Software Foundation; either 
     10version 2.1 of the License, or (at your option) any later version. 
     11 
     12This library is distributed in the hope that it will be useful, 
     13but WITHOUT ANY WARRANTY; without even the implied warranty of 
     14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
     15Lesser General Public License for more details. 
     16 
     17You should have received a copy of the GNU Lesser General Public 
     18License along with this library; if not, write to the Free Software 
     19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
     20 
     21Note: The LGPL licence can be found online at http://www.gnu.org 
     22 
     23*/ 
     24 
     25/** 
     26 * This example shows the use of enum types in Java 5.0  
     27 */ 
     28public class FruitSalad { 
     29 
     30    public enum FruitCategory { 
     31        SWEET, CITRUS, SMELLY 
     32    } 
     33 
     34    public enum Fruit { 
     35        APPLE { 
     36            FruitCategory getCategory() { 
     37                return FruitCategory.SWEET; 
     38            } 
     39        }, 
     40        ORANGE { 
     41            FruitCategory getCategory() { 
     42                return FruitCategory.CITRUS; 
     43            } 
     44        }, 
     45        GRAPEFRUIT { 
     46            FruitCategory getCategory() { 
     47                return FruitCategory.CITRUS; 
     48            } 
     49        }, 
     50        BANANA { 
     51            FruitCategory getCategory() { 
     52                return FruitCategory.SWEET; 
     53            } 
     54        }, 
     55        DURIAN { 
     56            FruitCategory getCategory() { 
     57                return FruitCategory.SMELLY; 
     58            } 
     59        }; 
     60 
     61        abstract FruitCategory getCategory(); 
     62    } 
     63 
     64    public static void main(String[] args) { 
     65        Fruit a = Fruit.APPLE; 
     66        System.out.println("The toString() for a: " + a); 
     67        System.out.println("a.getCategory() is: " + a.getCategory()); 
     68 
     69        for (Fruit f : Fruit.values()) { 
     70            System.out.println("Fruit is: " + f); 
     71        } 
     72    } 
     73} 
     74