Forums Register Login

Problem related to enums

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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..............
+Pie Number of slices to send: Send
okay...i got it.

Thank you very much guys for your help.
+Pie Number of slices to send: Send
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 ?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 (?)).
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
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#.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
We just need to memorize that the compiler replaces refToClassA.staticVar and refToClassA.staticMeth() with ClassA.staticVar and ClassA.staticMeth() respectively.

+Pie Number of slices to send: Send
That's interesting, I just tried this :



the output is 5.

now I feel a lot better

Thanks, Nitin !

+Pie Number of slices to send: Send
Glad that I made someone feel better !!
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3780 times.
Similar Threads
enum problem
can u please explain the ouput of the given code
What is the default value of an enum?
Explanation...
Interface question....
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:29:01.