Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Classic Enum example fails to compile

843793Dec 28 2003 — edited Jan 5 2004
Using the b31 alpha.

The classic Enum example that shows up in every article on Enums, fails to compile with the latest available alpha:
//
// exp1/Coin.java
//
package exp1;

public enum Coin {
  penny(1), nickel(5), dime(10), quarter(25);

  public Coin(int v) { this._value = v; }
  private final int _value;
  public int value() { return _value; }
}
// ====
//
// exp1/CoinTest.java
//
package exp1;

public class CoinTest {
  public static void main(String[] args) {
    for (Coin c : Coin.VALUES) {
      System.out.println(c + ":   \t" + c.value() +" \t" + color(c));
    }
  }
  private enum CoinColor { copper, nickel, silver }
  private static CoinColor color(Coin c) {
    switch(c) {
      case Coin.penny:   return CoinColor.copper;
      case Coin.nickel:  return CoinColor.nickel;
      case Coin.dime:
      case Coin.quarter: return CoinColor.silver;
      default: throw new AssertionError("Unknown coin: " + c);
    }
  }
}
Compiling this with the latest javac gives a bunch of errors:
% c:/java15/bin/javac -source 1.5 -d ../classes exp1/CoinTest.java
exp1/CoinTest.java:5: cannot find symbol
symbol  : variable VALUES
location: class exp1.Coin
    for (Coin c : Coin.VALUES) {
                      ^
exp1/CoinTest.java:12: an enum switch case label must be the unqualified name of an enumeration constant
      case Coin.penny:   return CoinColor.copper;
               ^
exp1/CoinTest.java:13: an enum switch case label must be the unqualified name of an enumeration constant
      case Coin.nickel:  return CoinColor.nickel;
               ^
exp1/CoinTest.java:13: duplicate case label
      case Coin.nickel:  return CoinColor.nickel;
      ^
exp1/CoinTest.java:14: an enum switch case label must be the unqualified name of an enumeration constant
      case Coin.dime:
               ^
exp1/CoinTest.java:14: duplicate case label
      case Coin.dime:
      ^
exp1/CoinTest.java:15: an enum switch case label must be the unqualified name of an enumeration constant
      case Coin.quarter: return CoinColor.silver;
               ^
exp1/CoinTest.java:15: duplicate case label
      case Coin.quarter: return CoinColor.silver;
      ^
8 errors
Has something changed in the way the feature was implemented, that makes the classic example wrong, or is this a bug in the compiler?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2004
Added on Dec 28 2003
6 comments
346 views