|
-
Dec 8th, 2004, 04:08 PM
#1
Enumeration
Is there any no enumeration in Java? Or something similar. Can't find anything...
ØØ
Last edited by NoteMe; Dec 11th, 2004 at 07:47 PM.
-
Dec 8th, 2004, 10:38 PM
#2
Dazed Member
Re: Enumeration
Yes Java 5.0 added enums which are now a type of their own.
-
Dec 8th, 2004, 11:03 PM
#3
Dazed Member
Re: Enumeration
I haven't checked them out though but probably a lot better than the old way.
Code:
/* Enum types the old style */
public class BlenderOldStyle{
public static final int SpeedSetting_off = 0;
public static final int SpeedSetting_low = 1;
public static final int SpeedSetting_med = 2;
public static final int SpeedSetting_high = 3;
}
-
Dec 9th, 2004, 01:13 AM
#4
Fanatic Member
Re: Enumeration
Code:
public class test{
public enum morons{noteme,
mendhak,
dsheller,
electroman,
plenderj,
unfortunately_me,
etcetera};
public test(){
System.out.println(morons.noteme.toString());
}
public static void main(String[] args){
new test();
}
}
-
Dec 9th, 2004, 11:48 AM
#5
Dazed Member
Re: Enumeration
November issue 11 vol 9 of JDJ has an interesting article on enums in jdk5.0. After getting half way through it seems that some ofthe sorce code is not listed in the magazine(Listings 2-9 can be dl from http://sys-con.com/story/?storyid=46981&DE=1#RES) but the link contains the full article and the source. Give it a read. It's sounds interesting.
-
Dec 9th, 2004, 04:13 PM
#6
Dazed Member
Re: Enumeration
I wanted to try out how enums work in the jdk5.0 so i did the following. Don't know if this is the right way to use them but i took a shot anyway.
Code:
/* Enum types the old style */
/*
class BlenderOldStyle{
public static final int SpeedSetting_off = 0;
public static final int SpeedSetting_low = 1;
public static final int SpeedSetting_med = 2;
public static final int SpeedSetting_high = 3;
private int currentsetting = SpeedSetting_off;
public void setSetting(int setting){
currentsetting = setting;
}
public void checkCurrentSetting(){
printCurrentSetting(currentsetting);
}
private void printCurrentSetting(int currentsetting){
switch(currentsetting){
case 0:
System.out.println("Blender is off");
break;
case 1:
System.out.println("Blender is on low");
break;
case 2:
System.out.println("Blender is on med");
break;
case 3:
System.out.println("Blender is on high");
break;
}
}
}
public class BlenderX{
public static void main(String[] args){
BlenderOldStyle bos = new BlenderOldStyle();
bos.checkCurrentSetting();
bos.setSetting(bos.SpeedSetting_med);
bos.checkCurrentSetting();
}
}
*/
/* new style jdk5.0 */
class Blender{
public enum SpeedState{Off,Low,Med,High};
private SpeedState currentState = SpeedState.Off;
public void setState(SpeedState state){
currentState = state ;
}
public SpeedState getNextSpeed(){
switch (currentState) {
case Off: return SpeedState.Off;
case Low: return SpeedState.Low;
case Med: return SpeedState.Med;
case High: return SpeedState.High;
}
return null ;
}
public void printStates() {
for(SpeedState state: SpeedState.values()){
System.out.println(state);
}
}
}
public class BlenderX{
public static void main(String[] args){
Blender bl = new Blender();
System.out.println(bl.getNextSpeed());
bl.setState(Blender.SpeedState.Med);
System.out.println(bl.getNextSpeed());
}
}
-
Dec 11th, 2004, 07:47 PM
#7
Re: Enumeration
Yeah, that was intresting, thanks. But they don't seem to have 5.0 at school, so no help actualy. So I had to do it the hard way. Thanks anyway.
ØØ
-
Dec 12th, 2004, 12:10 PM
#8
Dazed Member
Re: Enumeration
I actualy think using static final fields is a lot easier even if it does lack some of the benefits of having an enum type.
-
Dec 13th, 2004, 08:27 AM
#9
Re: Enumeration
 Originally Posted by Dilenger4
I actualy think using static final fields is a lot easier even if it does lack some of the benefits of having an enum type.
Much more to write, and I am used with Enums from C++, so it kind of irritates me. But nothing to do about it.
ØØ
-
Dec 13th, 2004, 08:48 AM
#10
Re: Enumeration
Enums provide type safety, string conversion, less writing, what else do you want?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 13th, 2004, 12:28 PM
#11
Dazed Member
Re: Enumeration
Posted by NoteMe
Much more to write, and I am used with Enums from C++, so it kind of irritates me. But nothing to do about it.
Sure there is somthing to do about it. Just use enums from java 5.
-
Dec 13th, 2004, 12:32 PM
#12
Re: Enumeration
Dilenger, he said he can't use Java 5 at school.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 14th, 2004, 04:06 PM
#13
Dazed Member
Re: Enumeration
Too bad.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|