aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explanation... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

Explanation...

Tamkin Avi
Greenhorn

Joined: Nov 09, 2008
Posts: 12
Hi all,


Given:

1. enum Animals {
2. DOG ("woof"), CAT ("meow"), FISH ("burble");
3. String sound;
4. Animals(String s) { sound = s; }
5. }
6. class TestEnum {
7. static Animals a;
8. public static void main(String[] args) {
9. System.out.println(a.DOG.sound + " " + a.FISH.sound);
10. }
11. }


I need Explanation....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Tamkin can you be more specific what you were unable to understand here.

As far as I know, people get confused how can you access DOG or FISH on a null reference.

If that's your doubt, then you can access DOG and FISH on reference a as DOG and FISH are static final fields of the enum. And you can access static members on a null reference. You must read a little bit about enums to understand how they are converted to normal classes after compilation...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tamkin Avi
Greenhorn

Joined: Nov 09, 2008
Posts: 12
Thanks, Ankit Garg
I have got the explanation.....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Explanation...