• 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

Kathy Sierra Chapter 1 Selftest question 4

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

I've just started studying for SCJP having completed chapter 1. I managed to get through the self test apart from question 4 which I thought would not compile due to an error on line 9 Here is the code: -



The reason I thought it would not compile is that line 9 is doing a.Dog and a.Fish instead of Animal.DOG and Animal.FISH. I'm unclear as to what variable a holds as it is uninitialised. I've put the code into eclipse and it gives me warnings on line 9: -


Would someone be able to clarify what is going on here?
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually the letter a would be replaced the word Animal here....accessing enums is just like accessing static inner classes
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Seager wrote:



above code is working absolutely fine for me, out put is : woof burble.
 
Edward Seager
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:actually the letter a would be replaced the word Animal here....accessing enums is just like accessing static inner classes



Ah, thanks for the info. I guess I will have to wait until Chapter 8 to fully understand how that works.

Minhaj kaimkhani wrote:
above code is working absolutely fine for me, out put is : woof burble.



Yes I know it compiles and runs, I was interested in why it didn't fail on line 9.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok....lets us make you understand it properly...
point number 1....whenever you try to access a static variable or a static method using an instance variable of a class..the name of that instance varibale is replaced by the class name even if that variable has a null value...so in this case "a" is replaced by the word "Animals"..and so you get the desired output....and another point is when you want to access an enum value you have access it using the class in which you put the enum in........but if your enum is outside any class you can access its value using just the enums name like Animals.woof instead of TestEnum.Animals.woof

clear now?
 
Edward Seager
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is a good explaination. So a is null but that doesn't matter, as it gets replaced by Animal. However using an instance variable instead of the class name produces warnings which I am seeing in Eclipse, but they don't matter either for the sake of the test.

Thanks. (I'll be back when I've read Chapter 2 :-) )
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya sure....am always here to provide any help
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic