Results 1 to 6 of 6
Thread: Learning enum
- 09-29-2011, 03:00 PM #1
Learning enum
Enums are useful when it comes to GUI(buttons). I dont know GUI yet, but I dont see a problem with learning enum before GUI.
A simple enum without a values/constructors like other programming languages enum types works fine. But this wont compile:
And the error message:Java Code:package pjjava.nbie; public enum Family { FATHER {56}, MOTHER {52}, SON {24}, DAUGHTER {21}, BABY {1}; private final int age; Familj (int age) { this.age = age; } }
Java Code:C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:5: error: illegal start of type FATHER {56}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:5: error: ';' expected FATHER {56}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:5: error: illegal start of type FATHER {56}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:6: error: ';' expected MOTHER {52}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:6: error: not a statement MOTHER {52}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:6: error: ';' expected MOTHER {52}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:6: error: illegal start of type MOTHER {52}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:6: error: ';' expected MOTHER {52}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:7: error: not a statement SON {24}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:7: error: ';' expected SON {24}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:7: error: illegal start of type SON {24}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:7: error: ';' expected SON {24}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:8: error: not a statement DAUGHTER {21}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:8: error: ';' expected DAUGHTER {21}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:8: error: illegal start of type DAUGHTER {21}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:8: error: ';' expected DAUGHTER {21}, ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:9: error: not a statement BABY {1}; ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:9: error: ';' expected BABY {1}; ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:13: error: invalid method declaration; return type required Familj (int age) ^ C:\Program\Java\jdk1.7.0\jre\classes\pjjava\nbie\Family.java:17: error: reached end of file while parsing } ^ 20 errors Tool completed with exit code 1Last edited by Pojahn_M; 09-29-2011 at 03:03 PM.
- 09-29-2011, 03:37 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Learning enum
Enums are useful in coding, not just GUI stuff. I have a stack of enums here, and my work is all back end server stuff.
Anywa, your problem is you should use (), not {}. They're essentially calls to the constructor:
ETA: Oh, and your constructor has a typo...the class is called Family, but the constructor has Familj.Java Code:FATHER (56), .. etc etc
- 09-29-2011, 04:52 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Learning enum
Tolls is very right, Enums are useful for a lot of stuff. Off the top of my head, I used enums in a card game, and an RPG.
- 09-29-2011, 07:50 PM #4
Re: Learning enum
Oh, my ( and ) looks almost like { and } in my web browser.
Anyway, enums was not hard. I have looked through many examples and coded them myself. They are pretty simple and I understand them.
But I cant think of many uses(except GUI buttons). Perhaps because I dont know everything in java yet.
Can someone give me a list of examples were enums are useful?
-
Re: Learning enum
Any place where you'd use several constants, substitute an enum.
- 09-29-2011, 10:38 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Learning enum
Java Code:private static enum PlayerClass{ //The class representations MAGE("Mage", 14, 12, 10, 10, 7, 5, 2, 2, Element.ACID), PRIEST("Priest", 13, 13, 10, 10, 6, 6, 2, 2, Element.WATER), WARRIOR("Warrior", 10, 14, 10, 12, 3, 7, 3, 3, Element.EARTH), ROGUE("Rogue", 10, 10, 22, 10, 1, 3, 9, 3, Element.LIGHTNING), HUNTER("Hunter", 10, 11, 14, 11, 2, 5, 6, 3, Element.AIR), DRUID("Druid", 10, 10, 12, 14, 1, 5, 3, 7, Element.FIRE); //The name of the class private String className; //Base stats of the class private int baseStamina; private int baseIntellect; private int baseStrength; private int baseAgility; //bonus stats gained per level of the class private int bonusIntellect; private int bonusStamina; private int bonusAgility; private int bonusStrength; //The type of the assigned class private Element type; private PlayerClass(String className, int baseIntellect, int baseStamina, int baseAgility, int baseStrength, int bonusIntellect, int bonusStamina, int bonusAgility, int bonusStrength, Element type ){ this.className = className; this.type = type; this.baseIntellect = baseIntellect; this.baseStamina = baseStamina; this.baseAgility = baseAgility; this.baseStrength = baseStrength; this.bonusIntellect = bonusIntellect; this.bonusStamina = bonusStamina; this.bonusAgility = bonusAgility; this.bonusStrength = bonusStrength; } }Is something recent where they use of enums were helpful.Java Code:public static Player newMage(String name){ return new Player(name, PlayerClass.MAGE, Race.HUMAN, Sex.MALE); } public static Player newPriest(String name){ return new Player(name, PlayerClass.PRIEST, Race.HUMAN, Sex.MALE); } public static Player newDruid(String name){ return new Player(name, PlayerClass.DRUID, Race.HUMAN, Sex.MALE); } public static Player newRogue(String name){ return new Player(name, PlayerClass.ROGUE, Race.HUMAN, Sex.FEMALE); } public static Player newHunter(String name){ return new Player(name, PlayerClass.HUNTER, Race.HUMAN, Sex.MALE); } public static Player newWarrior(String name){ return new Player(name, PlayerClass.WARRIOR, Race.HUMAN, Sex.MALE); }
Similar Threads
-
public static enum vs enum class
By Dipke in forum New To JavaReplies: 3Last Post: 08-30-2011, 10:45 AM -
hi guys help me please . Learning from pdf is good or learning from book ?
By funkygarzon in forum New To JavaReplies: 12Last Post: 06-14-2011, 04:55 PM -
Setting values from One Enum type to another enum type.
By reach2sudhakar in forum New To JavaReplies: 3Last Post: 09-23-2010, 06:02 PM -
enum
By billq in forum New To JavaReplies: 3Last Post: 01-03-2010, 08:38 PM -
Enum example
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:34 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks