Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Working with enum

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Here is a code using enums-


output of this code is-

null
aa
aa

My query is that bar reference has never been initialized. If it's value is null, how line no. 23 is able to compile? Why it's not throwing NullPointerException?
 
Greenhorn
Posts: 19
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be its a compiler syntax trick exactly like the following code.
t is null but still no NPE because referring to a static member through an instance variable is just a syntax trick. Compiler substitutes the instance variable with its Classname.

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arnab
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your enum declaration, each of the enum constant is implicitly declared as follows:


Think it will help you
 
Author
Posts: 375
19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Astha,

Try to decompile Test.class using a decompiler (like jad). Here's what you get:



This should help clear a lot of other related doubts.

With respect,
Mala
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic