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

enum semantics

 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, please give me a little help again.

I still do not seem to be comfortable with enum semantics. I thought the code below would not compile. What is it with this static member that seems to be not a enum member but the whole enum class?

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy gauchos,

in the line "que pasa" you declare a static enum variable of type Animals.
But you don't assign a value.
You can refer to this variable with Test.s_a because it is static.
Little hint:
Try this line as first line in your main method:

System.out.println(Test.s_a); // or just ...println(s_a);

Yours,
Bu.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this is confusing me even more:

 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Wentink:
Actually this is confusing me even more:



DOG && CAT are constant so when they're accessed, compiler will use the class name (here enum name) to replace the the reference variable. Therefore an object instance is not needed. In your example, the compiler will replace s_a.DOG to Animals.DOG. It's similar to the static variable of a class.
[ August 08, 2007: Message edited by: Yeming Hu ]
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
e if was not static variavel? it would launch an exception? NullPointerException?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aw, bad luck!

Marc said that
Actually this is confusing me even more


OK, you know already that the static variable s_a of class Test holds null in the first place.

But what you can do in a different class (of class Test itself - doesn't matter) is assigning enums of type Animals to that variable.

In your last example you only printed out the enums themself, you did not change the contents of the variable.
When you say System.out.println(s_a.DOG);
the s_a refers to Animals, and with the dot operator you select DOG. But this does not change the content of variable s_a.

Try this example:


it prints:
null
DOG
null
rereference
CAT
DOG
CAT


s_a is just a class variable (i.e. a static one) that can hold an element of Animal.
At the beginning it is null, as would be any "normal" object reference, when no object is assigned to it.
Then you refer the dog but not change the value of s_a.
Only in the bold line, you change the value.


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oi ga�chos (*),

Camilo asked
e if was not static variavel? it would launch an exception? NullPointerException?


Howdy, Camilo, welcome to the ranch!





It doesn't matter if it is static or not. But it will cause a NullPointerException only when you try to call a method (or field) of it:


NPE = NullPointerException.


See it?



Yours,
Bu.


---

(*) = Howdy, cowboys!
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand it now. This one looks cute also by they way:


[ August 08, 2007: Message edited by: Marc Wentink ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic