• 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

about enum

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code like this:

enum Animals {DOG, TIGER, CAT}
public class TestEnum {
public static void main(String[] args) {
Animals a = null;
System.out.println(a.DOG);
}
}
The result is : DOG

The question is: In my mind it will cause NullPointException because of the code Animals a = null;
who can tell me why the result comes from. Thanks a lot.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is my take on it...
DOG is a static const property of enum Animal.
Animals a = null simply asigned null to the instance of enum.

now a.DOG actually reads the static property of the enum DOG using an instance variable.
Thanks,
Ravi
 
yu yong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for Ravi.
I think this is the answer.
The code "enum Animals {DOG, TIGER, CAT}" == the code `class Animals{static final String DOG="DOG";static final String TIGER="TIGER";static final String CAT="CAT";}`
If this is currect then the question is clear.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can You please explain in detail.Since I am still confused that how a "null.Dog" returns Dog.Thanks in advance.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I am still confused that how a "null.Dog" returns Dog.Thanks in advance.
The compiler does not look at the reference itself (which is null), it looks at the declared type of the reference (which is Animals).
[ March 24, 2008: Message edited by: Irina Goble ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since static members of a class can be called using class name also and and enum variables are by default static, using the class type it will call it's variable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic