• Post Reply Bookmark Topic Watch Topic
  • New Topic

using enum with JDK1.5  RSS feed

 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have problem with using enum in JDK 1.5. Heres my code along with the main method and also the error generated by the compiler . Can anyone help/clarify?
------------------------------------------------------------
import java.util.*;
enum Coin{
penny(1), nickel(5), dime(10),quarter(25);
private final int value;
Coin(int value) {
this.value =value;
}
public int value() {
return value;
}
}
class CoinTest{
public static void main(String a[]){
for(Coin c : Coin.VALUES){
System.out.printf("Coin "+c+" value is"+c.value());
}
}
}
--------------------------------------------------------------------
compiler error:
D:\>javac -source 1.5 CoinTest.java
CoinTest.java:14: cannot find symbol
symbol : variable VALUES
location: class Coin
for(Coin c : Coin.VALUES){
^
1 error
---------------------------------------------------
why does it say - cannot find VALUES,
the VALUES field must be automatically generated by the enum class i thought!
thanks
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use like this... enum does nothave VALUES constant to represent the values in it...We have to use static method of enum... The static method values() returns a list of all the enum constants... Hope this helps...
 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, that worked! But i got the example from Joshua Blochs article on sun web site which uses the VALUES field, not the values() method. Any idea abt that?
regards,
meenakshi
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by meenakshi ashokkumar:
But i got the example from Joshua Blochs article on sun web site which uses the VALUES field, not the values() method. Any idea abt that?

meenakshi, I guess that's because it's during the state of developing beta and some constants might not be consistent... But I'm not sure about that much.... I like such prorgamming challenge... I used to debug and correct the errors for my friends' codes in the past... But these days, I got bunch of codes to do and no more time to correct others' code... but I am really interested in Tiger.. That's why I'm following it... Any other code snippets unsolved are welcome... I'll try to investigate, if I got time...
 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help. will let u know if i get stuck again.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by meenakshi ashokkumar:
But i got the example from Joshua Blochs article on sun web site which uses the VALUES field, not the values() method. Any idea abt that?

As the other poster guessed, the implementation changed since the time Bloch's earlier article was published. Another thing that has changed: you no longer specify the enum type name in each case label of a switch statement:

Also, if I recall correctly, the name and ordinal fields used to be exposed directly but now you must access them through public instance methods. http://www.langrsoft.com/articles/enum.html
Jeff
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Langr:
Also, if I recall correctly, the name and ordinal fields used to be exposed directly but now you must access them through public instance methods. http://www.langrsoft.com/articles/enum.html

Thank you very much, Mr.Jeff, for such informative article... I have just read it and I got some knowledge from it... Thanks...
 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Jeff! thanks a lot for that information. I sure learnt a lot too.!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!