• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Enum

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


- Why is the call to Suits.SPADES.points coming back as an array (i.e. @bf73fa) rather than int value?

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok .. my mistake. The array is returned from the call to Suits.values().
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:Ok .. my mistake. The array is returned from the call to Suits.values().


where is your toString() method?

Also, I think that it is an error to try to access private fields from the main method:

This only works since main is within the Suits body. Better perhaps is to give Suits a public getPoints() method or to display points in the Suits toString() method.

Cheers!
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is correct. The output is shown below



This came from question 7 in Chapter 3. This is one of the question that had me scratching my head several times
 
Stephan van Hulst
Bartender
Pie
Posts: 5594
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete means it's bad programming, not necessarily an error. You should refrain from accessing private fields in the main method, and arguably in other static methods.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
array is an object, you should iterate it to retrieve the objects/primitive in it. This is same as,


Enum values() method returns an array of Enum constants. So, other than printing the array directly, you should iterate within the array. And further as said by pete stein, you should override the toString() method of the Enum, other wise, you will get the same as you got for printing the array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic