wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes declaring Enum variable as Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "declaring Enum variable as "final" " Watch "declaring Enum variable as "final" " New topic
Author

declaring Enum variable as "final"

Shiraz Choudhary
Greenhorn

Joined: Aug 20, 2008
Posts: 4
Thanks Guys for the hlep, but my real question was why an "enum" type variable refering to a null object is still able to get its constant by using the dot(.) notation, and all the other object type can't do that. Like in the code below:

The output is:
"Monday
Test Class"
NullPointerException for statement "Sys out.prtn(dd.Monday);

The enum type variable is going to print the value "Monday" and then you get the "Test Class", but "dd.Monday" expression gives you NullPointerException, which i understand very well. I just can't understand why the code

is not throwing the NullPointerException.

[ September 11, 2008: Message edited by: Shiraz Choudhary ]
[ September 11, 2008: Message edited by: Shiraz Choudhary ]

SCJP 6.0
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
Hello Shiraz

I am also having same drought now but, as per me Day is static so compile object is replace by Class name. like

public static void main(String args[]) {
System.out.println(Day.Monday);
}
But not sure.. still we need review of more ranchers.


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
i also tried you case by this way

enum Day{Monday, Sunday}
public class Test{

public static void main(String[] args){
final Day d = null;
System.out.println(d.Monday);
}
}
but still Day is not static compile time it`s converted with .

System.out.println(Day.Monday);

is this case with normal.. can any one can tell what rules are applying here... how compiler treating enum variables.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Yes! enum constants are by default final and static. If you access anything that is static with an instance of the enclosing type(i.e. class or enum), then the compiler replaces the instance name with the enclosing type name...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shiraz Choudhary
Greenhorn

Joined: Aug 20, 2008
Posts: 4
Another Question:
Can enum class ever be declared as "abstract"?
Lynne Forr
Greenhorn

Joined: Sep 05, 2008
Posts: 3
Another Question:
Can enum class ever be declared as "abstract"?


No, because enums are by default final. Things cannot be marked final and abstract, as it is a contradiction to their meanings.

L.
Nya Iwa
Greenhorn

Joined: Sep 01, 2008
Posts: 6
Originally posted by Shiraz Choudhary:
Another Question:
Can enum class ever be declared as "abstract"?


Originally posted by Jesper Young:
[qb]You could regard an enum as a special kind of class.

You can't extend an enum with another normal class - that would be very strange. What would a class that extends an enum be - some strange kind of enum with extra stuff added?

You also can't extend an enum with another enum. This page from Sun's tutorials explains why:

Note: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.


[ September 12, 2008: Message edited by: Nya Iwa ]
[ September 12, 2008: Message edited by: Nya Iwa ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring Enum variable as "final"
 
Similar Threads
Where do enums live?
Calling a method from another class
Why Enum type are not extended futher
Poker
null as key in HashMap ?