Java
  Home arrow Java arrow Page 2 - Java Comes of Age
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Comes of Age
By: Simon White
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2004-04-07

    Table of Contents:
  • Java Comes of Age
  • Type-Safe Enumerations
  • Static Import and Generics
  • Enhanced For Loop
  • Auto-boxing, Auto-Unboxing, and VarArgs
  • Meta-data

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Java Comes of Age - Type-Safe Enumerations


    (Page 2 of 6 )

    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 Articles
    More By Simon White


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway