• Post Reply Bookmark Topic Watch Topic
  • New Topic

How use Enum in Java 6.0  RSS feed

 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to use enum in java 6.0. The following is the code but it gives error. please any body can guide me why it is giving error.
The error is enum is a keyword and not be used as identifier....Any body can help me why is this error

public class EnumTest {

public enum MainMenu {FILE, EDIT, FORMAT, VIEW};

public static void main(String[] args) {

for (MainMenu menu : MainMenu.values())
{ System.out.println(menu); }



}
}
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the semi-colon after your enum statement.
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i removed the semicolon it is still giving error, looks like compiler in java 6 do not support enum.....
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse the Java 6.0 compiler supports enum, you're just doing something wrong.

Are you sure you are using Java 6.0? Do you have some other version of the JDK in your PATH before the Java 6 JDK? Try typing this into a command prompt window:

java -version

What version number does it show?

Are you using an IDE like Eclipse? If so, did you configure it correctly so that it knows you are writing Java 5.0 or newer code?
[ October 06, 2006: Message edited by: Jesper Young ]
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using java 6.0 and using the netbeans to write my application.
The above is the complete code for enum. If you read the whole code i do not understand why compiler is giving error. please any body can help because my code is not compiling.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with the code.

Even though you may be using JDK 6 to run NetBeans (and you haven't told us what you've done to confirm this!) this does not mean that you have NetBeans configured to compile code for JDK 6.0. Your "JDK Compliance" settings may be set to accept only code for JDK 1.4 or earlier.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with your code (with or without the semi-colon after your enum). So as Jesper says it must be something to do with your environment. Is netbeans configured to use JSE5.0 or above?
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i check if my netbeans is setup for jdk 5 and above
canu pls guide me how to check in netbeans.
even if it is set up for jdk 1.4 and above it should work for enum.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alix Sye:

even if it is set up for jdk 1.4 and above it should work for enum.


No -- "enum" was introduced for the first time in JDK 1.5.
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please some body help me, my compiler do not compile enums.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you still doing this in NetBeans? Have you configured NetBeans so that it compiles your code as Java 5.0 or 6.0 code, as was discussed above?

Do the following things first. As long as you don't do them, we're not going to get any further with your problem.

Try compiling your code in a command prompt window. Use a command like this:

javac MyClass.java

See if the compiler complains about the enums. If it does, type in the following command:

javac -version

Does it tell you you're using JDK 1.6 or not? (Note: The "-version" switch was added in version 1.5 of the Java compiler. If it doesn't tell you a version number, you're using a Java version 1.4 compiler or older).
[ October 11, 2006: Message edited by: Jesper Young ]
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need help to figure out why error is coming
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, my friend. I've personally only used NetBeans for one afternoon, about six months ago; found it so abhorrent that I've never looked at it again. But I went to Google and typed in "Netbeans compiler compliance level". On the search results page, the excerpt for the fifth hit included the following menu path for setting the compiler compliance level in NetBeans:

Window > Preferences > Java > Compiler

Please post your mail address so I can send you a bill for my consulting services.
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
OK, my friend. I've personally only used NetBeans for one afternoon, about six months ago; found it so abhorrent that I've never looked at it again. But I went to Google and typed in "Netbeans compiler compliance level". On the search results page, the excerpt for the fifth hit included the following menu path for setting the compiler compliance level in NetBeans:

Window > Preferences > Java > Compiler

Please post your mail address so I can send you a bill for my consulting services.


hehe..are you serious about billing for your consult?

anyway, there is nothng wrong with the coding above, as i compiled it with java1.5,and why topic starter keeps want it to be compiled using netbeans?
Netbeans a little complicated, just use command prompt window lol.
[ October 20, 2006: Message edited by: Nicky Eng ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would venture a guess that they "want" to use NetBeans. Of course, we really have to wonder why if after all this time they still haven't managed to figure out how to use NetBeans.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alix Sye:
need help to figure out why error is coming

Alix, if you want us to help you solve your problem, you have to be more cooperative. Right now, you're not cooperating to solve your problem.

If you're not willing to do what we ask you (like typing in "javac -version" in a command prompt window), then it is impossible to help you.
[ October 21, 2006: Message edited by: Jesper Young ]
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i typed in command prompt window javac -version it gave the following answer


javac 1.6.0-beta2


still cannot compile enums
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix,

This is one of these bad-dream kind of threads that just won't die. People have told you time and again that it's the language compliance setting in NetBeans that matters, not what JDK you're running (except for Jesper, who, uncharacteristically, doesn't seem to be paying attention.) When you refused to try to deal with this I went and Googled how to do it for you, a few posts back. To get to the appropriate control panel, you navigate:

Window > Preferences > Java > Compiler

And choose a language compliance setting of Java 5 or above. Once you've done that, your problem will disappear.

Please do it, so this thread can be put out of its misery.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a few posts above, I tried to have Alix compile his/her source file on the command line, making sure that he/she is indeed using javac version 1.6, but he/she doesn't seem to be willing to try that either...
 
Alix Sye
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i went into netbeans and try to find the following but could not find it.

Window > Preferences > Java > Compiler

In net beans Under window there is NO Preferences option.

which window you guys are refering to i was looking in netbeans under window but could not find option for Preferences.

Thanks Alix
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I told you, I found that via Google, which can't compare to having the darn thing right in front of you.

I found somebody who has NetBeans 5 installed and looked at it for a second. I found that with a Java project open, you can select "<ProjectName> Properties" from the File menu; then choose the "Sources" category from the left-hand pane of the dialog that comes up. At the bottom of the window, you should see a popup menu that says "Source Level". Make sure it says "1.5" or higher. Press "OK".

OK?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!