• 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

Typo in Mcgraw-Hill SCJP 5 Book?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic