• 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

Problem related to enums

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi....I have started preparing for SCJP 6 from k & b... book. I have done the 1st chapter and give the self test and found one problem in question no. 4.

The question is :

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. }
What is the result?

And the answer is:

woof burble

I did not understand the concept of using " static Animals a; ". Anyone please help me.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rohit,
The point of that line is to declare a reference that can be used in a static method such as main()
The usage of a.DOG.sound and a.FISH.sound in line 9 are references via the class of a to the public instance variable sound of the enum instances, they could equivalently have been written as Animals.DOG.sound and Animals.FISH.sound and with line 7 commented out
 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit Sidana wrote:Hi....I have started preparing for SCJP 6 from k & b... book. I have done the 1st chapter and give the self test and found one problem in question no. 4.

The question is :

Given:


What is the result?

And the answer is:

woof burble

I did not understand the concept of using " static Animals a; ". Anyone please help me.


Hi Rohit Sidana,
Here what happening in your code is :

The 'enum Animals' have three values namely DOG, CAT, FISH . Generally , the simple enum looks like 'enum Animals{DOG,CAT,FISH}'. But, like classes , enums also can have instance variables,constructors and methods . your code is showing the second type.
Now , in your code , the elements of enum Animals (DOG,CAT and FISH) are actually treated as instances of Animals. Here, since these instances are supplied with values (one for each) , the constructor is invoked automatically for each one. So, for instance , consider DOG("woof") . here on processing of DOG , constructor Animals("woof") is automatically executed and now Animals.DOG.sound gives "woof" .
In your code, there is a 'static Animals a ;' declaration . since we are accessing 'a' from static context (static main) , we declare it as static. So, as i said above, if you print the value of 'a.DOG.sound', it shows 'woof' on the screen.Similarly happens for a.FISH.sound, which prints burble.

I hope this gives you clear understanding..............
 
Rohit Sidana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay...i got it.

Thank you very much guys for your help.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still confused with :



I ran the code, debugged it, and the outcome is "woof burble". But still, null.something confuses me a lot.

Could someone please clarify this in one or two sentences ? Why don't we get a NullPointerException ?

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This confused me, too, so I did some reading. Here's what I learned: enum constants are implicitly static. As we know, Java allows you to access a static field with an instance reference. But it doesn't matter if the instance is null because the static field belongs to the type, not the instance.
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:This confused me, too, so I did some reading. Here's what I learned: enum constants are implicitly static. As we know, Java allows you to access a static field with an instance reference. But it doesn't matter if the instance is null because the static field belongs to the type, not the instance.




To add to Dennis' reply, the compiler substitutes a.DOG.sound with Animals.DOG.sound as it considers DOG as a static object belonging to the Animals class. So, the JVM would not be able to see a.DOG.sound. Hence, a being null is immaterial in this case and the NullPointerException would not be thrown.
 
Mehmet Gunacti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you explanations.

So I just take it as a rule then; enum's can never throw NullPointerExceptions ! (since they are not considered objects (?)).
 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mehmet Gunacti wrote:Thanks for you explanations.

So I just take it as a rule then; enum's can never throw NullPointerExceptions ! (since they are not considered objects (?)).



Hi Mehmet,

CAT, DOG and FISH are all (implicitly static) objects of enum type Animals. So, we can't say enums are not considered objects.
We can also get a NullPointerException in cases like the one below wherein we are trying to use the instance method getSound() using a null reference a.



Cheers,
Nitin Sethi

 
Mehmet Gunacti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,

I just searched for every occurrence of the word 'enum' in the java spec 3.0, but I'm not very good at reading the java specs, and I couldn't find a rule for that behavior.
So I'm still confused.

I get your point with the instance method getSound().

Your explanation clarifies it best :

the compiler substitutes a.DOG.sound with Animals.DOG.sound as it considers DOG as a static object belonging to the Animals class. So, the JVM would not be able to see a.DOG.sound. Hence, a being null is immaterial in this case and the NullPointerException would not be thrown.



IMHO they should have prevented the invocation of static methods / fields over a reference, like in C#.
 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mehmet,

I beg your pardon but I could not get what your confusion now is.

I do agree to your opinion. Things like this make Java even more complex.
 
Mehmet Gunacti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, it's just strange that null.someoperation is valid in Java.

I won't write code similar to that for sure, because it's everything but readable.

For the exam, simply memorizing this as a rule should be the best I think.
 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We just need to memorize that the compiler replaces refToClassA.staticVar and refToClassA.staticMeth() with ClassA.staticVar and ClassA.staticMeth() respectively.

 
Mehmet Gunacti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's interesting, I just tried this :



the output is 5.

now I feel a lot better

Thanks, Nitin !

 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad that I made someone feel better !!
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic