License Lesser General Public License, version 2.1
Lines 61
Keywords
enum (3)
Included in this Library
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users
Hide
Siafoo – the intersection of pastebin, help desk, version control, and social networking Join Siafoo Now or Learn More

Java Enum example Atom Feed 0

In Brief A self contained example of creating and using enums in Java 1.5
# 's
 1//package com.wickedcooljava.core;
2
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}

A self contained example of creating and using enums in Java 1.5

Comments

over 7 years ago (23 Nov 2008 at 03:33 PM) by pixelmonkey
This example doesn't seem very good. You're creating separate anonymous class definitions for each enum constant for no reason. You could just as easily support the same thing with far less code and far less bloat with the following:

public class FruitSalad {

public enum FruitCategory {
SWEET, CITRUS, SMELLY
}

public enum Fruit {
APPLE(FruitCategory.SWEET),
// ... other constants with categories passed to ctor
}

private FruitSalad(FruitCategory category) { this.category = category; }
private FruitCategory category;
public FruitCategory getCategory() { return this.category; }
}
over 7 years ago (23 Nov 2008 at 10:19 PM) by Stou S.
I took the example from the book Wicked Cool Java and I think that the code isn't supposed to be that great and/or efficient, just a clear example of how enum's work in Java.

I strongly encouraged you to either edit this snippet or post another snippet with your code =)