Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Enum" Watch "Question on Enum" New topic
Author

Question on Enum

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



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

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok .. my mistake. The array is returned from the call to Suits.values().
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 02, 2005
Posts: 430

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

Joined: Sep 20, 2010
Posts: 3394
    
    9
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

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Enum
 
Similar Threads
K & B, Chapter 3, Quiz Question 7
K&B Chapter 3 Question 7
Sun Ceritified Programmer for Java 6 Study Guide chapter-3 & question-7
Should it not give compile time error?
Info concerning the updates to the SCJP 6 exam