| Author |
enum is Java keyword ?
|
TH Eee
Greenhorn
Joined: Dec 22, 2006
Posts: 6
|
|
Hi, I wonder that when I try some mock exams especially the question relate to keyword , 'enum' is not treated as keyword. When I check the list of keyword provided by sun website, http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html enum is included. My question is 'enum' a keyword ? Thank You.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Sounds like the mock exams your are trying are for a pre-1.5 version of Java. enmus were introduced in that version.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Neerav Narielwala
Ranch Hand
Joined: Dec 08, 2006
Posts: 106
|
|
In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time�for example, the choices on a menu, command line flags, and so on. Here is some code that shows you how to use the Day enum defined above: public class EnumTest { Day day; public EnumTest(Day day) { this.day = day; } public void tellItLikeItIs() { switch (day) { case MONDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } } public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); thirdDay.tellItLikeItIs(); EnumTest fifthDay = new EnumTest(Day.FRIDAY); fifthDay.tellItLikeItIs(); EnumTest sixthDay = new EnumTest(Day.SATURDAY); sixthDay.tellItLikeItIs(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellItLikeItIs(); } } The output is: Mondays are bad. Midweek days are so-so. Fridays are better. Weekends are best. Weekends are best. Java programming language enum types are much more powerful than their counterparts in other languages.
|
<a href="http://www.java-tips.org/java-tutorials/tutorials/" target="_blank" rel="nofollow">Java Tutorials</a> | <a href="http://www.planet-java.org" target="_blank" rel="nofollow">Java Weblog</a> | <a href="http://computer-engineering.science-tips.org" target="_blank" rel="nofollow">Computing Articles</a>
|
 |
 |
|
|
subject: enum is Java keyword ?
|
|
|