Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B chapter 3 Question 7 enum.values() method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am really getting confused while comparing functionality of Enum's .values() method in Q7 of Chapter 3 Versus enum example in Chapter 1 page 64.
Please help..

Q7:



Here Suits.values() gives a reference value like @bf73fa

Example :



Which produces:
8
BIG 8
HUGE 10
OVERWHELMING 16

Why in the example, BIG,HUGE.. are printed and in Q7 a reference value is printed? what am i missing here?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enum tutorial over at Oracle states:

Enum Types Tutorial wrote:
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.


Now that we've established that the values() method returns an array, take a look at the two code samples you provided and look at how they handle the return value of the call to the values() method. Can you explain what's happening in either case?
 
Rageni Kalra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now when you say it like that, i feel so stupid
Q7 is just printing the array reference and in the example, we are iterating over the array returned by .values()

thanks alot
 
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rageni Kalra wrote:



Here Suits.values() gives a reference value like @bf73fa



Although, as it stands, it won't compile. There is a printing error in the book at line 16. It should use the getValue() method.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic