Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explanation...

 
Tamkin Avi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Tamkin Avi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ankit Garg
I have got the explanation.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic