Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: Learning enum

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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:

    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;
    	}
    }
    And the error message:

    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 1
    Last edited by Pojahn_M; 09-29-2011 at 03:03 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Learning enum

    Quote Originally Posted by Pojahn_M View Post
    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.
    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:
    Java Code:
        FATHER (56),
    .. etc etc
    ETA: Oh, and your constructor has a typo...the class is called Family, but the constructor has Familj.
    sunde887 likes this.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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;		
    		}
    	}
    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); }
    Is something recent where they use of enums were helpful.

Similar Threads

  1. public static enum vs enum class
    By Dipke in forum New To Java
    Replies: 3
    Last Post: 08-30-2011, 10:45 AM
  2. Replies: 12
    Last Post: 06-14-2011, 04:55 PM
  3. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  4. enum
    By billq in forum New To Java
    Replies: 3
    Last Post: 01-03-2010, 08:38 PM
  5. Enum example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •