aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Typo in Mcgraw-Hill SCJP 5 Book? 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 "Typo in Mcgraw-Hill SCJP 5 Book?" Watch "Typo in Mcgraw-Hill SCJP 5 Book?" New topic
Author

Typo in Mcgraw-Hill SCJP 5 Book?

Roger Orange
Greenhorn

Joined: Jan 28, 2009
Posts: 7
Are instance variables in constant specific class bodies public by default or is there a typo in this sample exam question?
8. Given:


What is the result?
A. woof burble
B. Multiple compilation errors
C. Compilation fails due to an error on line 2
D. Compilation fails due to an error on line 3
E. Compilation fails due to an error on line 4
F. Compilation fails due to an error on line 9
Answer:
® 3 A is correct; enums can have constructors and variables.
®˚ B, C, D, E, and F are incorrect; these lines all use correct syntax



cheers Todd
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Have you tried putting the enum and the test class in a different package and trying to compile the test class then? By the way, the sound instance variable is not an instance variable of the constant specific class body (I don't know what that would mean.) It is an instance variable of the enum type Animals. Let me know if this doesn't make things clear.


All code in my posts, unless a source is explicitly mentioned, is my own.
Roger Orange
Greenhorn

Joined: Jan 28, 2009
Posts: 7
Ruben Soto wrote:Have you tried putting the enum and the test class in a different package and trying to compile the test class then? By the way, the sound instance variable is not an instance variable of the constant specific class body (I don't know what that would mean.) It is an instance variable of the enum type Animals. Let me know if this doesn't make things clear.

Th x Ruben

It compiles, with the warning static fields should be accessed in a static way (Animals.DOG, Animals.FISH). The syntax on line 9 confused me. Constant specific class bodies allow you to override the methods defined in the body of the enum declaration for a particular enum constant.


cheers Todd
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Todd,

What I meant is that since sound has default access, if you move the Animals enum to its own file in a different package from TestEnum, you won't be able to compile TestEnum, since it will be unable to access the sound variable. That was to answer your original question of whether variables defined in an enum were made public by default.

And the warning you get is normal. It's just a question of style, nothing more.

Regards,

Ruben
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Typo in Mcgraw-Hill SCJP 5 Book?
 
Similar Threads
mock question about "case"
Enums
Problem related to enums
Chapter 1 Question 8 Kathy & Bates book.
Doubt from K&B ch 5