Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explanation..." Watch "Explanation..." New topic


Tamkin Avi

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


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

Joined: Aug 03, 2008
Posts: 9465

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

Joined: Nov 09, 2008
Posts: 12
Thanks, Ankit Garg
I have got the explanation.....
I agree. Here's the link:
subject: Explanation...
It's not a secret anymore!