Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Typo in Mcgraw-Hill SCJP 5 Book?

 
Roger Orange
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roger Orange
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic