A SCJP question about Java enumerations

Given the following:

1.public enum Wallpaper {
2.  BROWN, BLUE, YELLOW;
3.}

Which of the following are legal?

  1. enum PatternedWallpaper extends Wallpaper {
    STRIPES, DOTS, PLAIN;
    }
  2. Wallpaper wp = Wallpaper.BLUE;
  3. Wallpaper wp = new Wallpaper(Wallpaper.BLUE);
  4. void aMethod(Wallpaper wp) {
    System.out.println(wp);
    }
  5. int hcode = Wallpaper.BLUE.hashCode();

Answer: only items 2, 4 and 6 are corrects. We can“t extend or instantiate an enumeration. The item 2 is the correct way to get a enumeration reference, the item 4 shows a method receiving a enumeration reference, and finally item 6 shows a call for the hashCode() method that all enums inherit from Object.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Furl
  • Ma.gnolia
  • Reddit
  • Slashdot
  • Technorati
  • YahooMyWeb
  • BlinkList
  • Netscape
  • NewsVine
  • StumbleUpon
  • TailRank
  • blinkbits
  • blogmarks
  • BlogMemes
  • co.mments
  • DotNetKicks
  • MisterWong
  • Smarking

One Response to “A SCJP question about Java enumerations”

  1. marcel sauer Says:

    hi,

    answer 6 is not available ;-)

    anyway - nice blog

    marcel

Leave a Reply