• 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

declaring Enum variable as "final"

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Shiraz Choudhary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another Question:
Can enum class ever be declared as "abstract"?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic