Click to See Complete Forum and Search --> : Enumeration
NoteMe
Dec 8th, 2004, 03:08 PM
Is there any no enumeration in Java? Or something similar. Can't find anything...:(
ии
Dillinger4
Dec 8th, 2004, 09:38 PM
Yes Java 5.0 added enums which are now a type of their own.
Dillinger4
Dec 8th, 2004, 10:03 PM
I haven't checked them out though but probably a lot better than the old way.
/* 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;
}
brown monkey
Dec 9th, 2004, 12:13 AM
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();
}
}
:D
Dillinger4
Dec 9th, 2004, 10:48 AM
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.
Dillinger4
Dec 9th, 2004, 03:13 PM
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.
/* 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());
}
}
NoteMe
Dec 11th, 2004, 06:47 PM
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.
ии
Dillinger4
Dec 12th, 2004, 11:10 AM
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.
NoteMe
Dec 13th, 2004, 07:27 AM
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.
ии
CornedBee
Dec 13th, 2004, 07:48 AM
Enums provide type safety, string conversion, less writing, what else do you want?
Dillinger4
Dec 13th, 2004, 11:28 AM
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.
:ehh: Sure there is somthing to do about it. Just use enums from java 5.
CornedBee
Dec 13th, 2004, 11:32 AM
Dilenger, he said he can't use Java 5 at school.
Dillinger4
Dec 14th, 2004, 03:06 PM
Too bad. :(
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.