Home arrow Java & J2EE arrow Page 2 - Java Comes of Age

Type-Safe Enumerations - Java

Time to open the Champagne -- Java 1.5 is out, and the language has finally come of age! With the new Java 1.5 specification, Java now contains features that make it feel like a proper "grown-up" language. The rest of this article will introduce you to these new features. To try out the features for yourself, simply download Java 1.5 from Sun’s website and give it a whirl. Note that you’ll need to compile the code using the –source 1.5 option; otherwise, you’ll get compilation errors when using the new features.

TABLE OF CONTENTS:
  1. Java Comes of Age
  2. Type-Safe Enumerations
  3. Static Import and Generics
  4. Enhanced For Loop
  5. Auto-boxing, Auto-Unboxing, and VarArgs
  6. Meta-data
By: Simon White
Rating: starstarstarstarstar / 20
April 07, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Personally, the omission of "proper" enumerations in earlier versions of Java really annoyed me. I still can't believe how Java survived so long without it. In versions of Java prior to 1.5, the typical approach to represent the suit of a playing card would be something like the following:


public interface CardSuit {
    
public static final int HEARTS   0;
    
public static final int SPADES   1;
    
public static final int CLUBS    2;
    
public static final int DIAMONDS 3;
}

The problem is that this representation is not type-safe: it is too easy to mix up an int that represents the suit of a card with another int such as a loop counter. It could be made type-safe by writing a class with a private constructor:


public class CardSuit {
    
public static final Suit HEARTS   = new Suit();
    
public static final Suit SPADES   = new Suit();
    
public static final Suit CLUBS    = new Suit();
    
public static final Suit DIAMONDS = new Suit();
 
    private 
Suit() {}
}

In this case, the only instances of the class Suit that will ever exist are those that are created inside the class itself. It guarantees type-safety, but is long-winded and not easy to read. In Java 1.5, you can create an enumerated type in a single line as follows:


public enum CardSuit heartsspadesclubsdiamonds };

Think of the enum keyword as an alternative to class, as similar restrictions apply. As with classes, public enumerations must be in a file named after the enumeration, but the same restriction does not apply to package level enumerations. Inner enumerations (if that is the correct terminology) are also permitted, and can be public:


package card;
 
public class 
Card {
    
public enum CardValue acetwothreefourfivesixseveneightninetenjackqueenking };
 
    private 
CardSuit  suit;
    
private CardValue value;    
 
    public 
Card(CardValue newValueCardSuit newSuit) {
        suit  
newSuit;
        value 
newValue;
    
}
 
    public 
CardSuit getSuit() {
        
return suit;
    
}    
 
    public 
CardValue getValue() {
        
return value;
    
}
 
    public 
String toString() {
        
return value+" of "+suit;
    
}
}

You can refer to the inner enumeration and its allowed values using the dot notation: Card.CardValue.ace, for example.



 
 
>>> More Java & J2EE Articles          >>> More By Simon White
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: