This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum semantics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum semantics" Watch "enum semantics" New topic
Author

enum semantics

Marc Wentink
Ranch Hand

Joined: May 18, 2007
Posts: 142
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?



SCJP5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Marc Wentink
Ranch Hand

Joined: May 18, 2007
Posts: 142
Actually this is confusing me even more:

Yeming Hu
Ranch Hand

Joined: May 14, 2007
Posts: 37
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 ]

Best Wishes,<br /> Yeming
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
e if was not static variavel? it would launch an exception? NullPointerException?


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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

Joined: Aug 25, 2006
Posts: 1274
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

Joined: May 18, 2007
Posts: 142
I think I understand it now. This one looks cute also by they way:


[ August 08, 2007: Message edited by: Marc Wentink ]
 
wood burning stoves
 
subject: enum semantics
 
Similar Threads
Kathy Sierra Chapter 1 Selftest question 4
What is the default value of an enum?
SCJP by KS & BB: Static access to non-static enum
Typo in Mcgraw-Hill SCJP 5 Book?
Problem related to enums