• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Question on Enum

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok .. my mistake. The array is returned from the call to Suits.values().
 
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 VI Editor Debian
  • 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
 
Saloon Keeper
Posts: 10308
217
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure 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
Boost this thread!