Results 1 to 3 of 3

Thread: Enum confusion

  1. #1
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default Enum confusion

    Hey, I've just been playing around with enums and I was wondering why
    the following doesn't work. I've found out a way to get it to work by just copying an example I found but I don't understand how it works. Could
    someone please explain why you have to use the static{ } keyword like that with a static method to retrieve the map before you can put enums in it. This is the first time I've seen the static modifier used this way. If there is any good material you can provide me I'd be happy to read, but I just don't know what this type of thing is. I also noticed that I can declare variables the same way I did with dayMap, but if I type:

    Java Code:
    int vv = 4;
    int val = 7;
    val = 21;
    I will get an error. Syntax error on token ";", , expected on the previous line

    Broken Example:
    Java Code:
    import java.util.Map;
    import java.util.TreeMap;
    
    public enum Day2 {
    
    	DAY1STRING1("Open"),
    	DAY1STRING2("Closed"),
    	DAY1STRING3("Cancelled"),
    	DAY1STRING4("Reversed"),
    	
    	DAY2DOUBLE1(2.2),
    	DAY2DOUBLE2(5.0);
       
        public Map<String, Day2> dayMap = new TreeMap<String, Day2>();
       
        dayMap.put("Open", DAY1STRING1);
        dayMap.put("Closed",DAY1STRING2);
        dayMap.put("Cancelled",DAY1STRING3);
        dayMap.put("Reversed", DAY1STRING4);
        dayMap.put("Reversal",DAY2DOUBLE1);
        dayMap.put("Expired", DAY2DOUBLE2);
    
        private Day2(String value){
        	this.value = value;
        } 
       
        private Day2(Double value){
        	this.num = value;
        }
    }

    Working Example:
    Java Code:
    import java.util.Map;
    import java.util.TreeMap;
    
    public enum Day2 {
    	//Day2 enums use the Day2 constructor for String types. 
    	DAY1STRING1("Open"),
    	DAY1STRING2("Closed"),
    	DAY1STRING3("Cancelled"),
    	DAY1STRING4("Reversed"),
     
    	DAY2DOUBLE1(2.2),
    	DAY2DOUBLE2(5.0);
        
        public static Map<String, Day2> dayMap = new TreeMap<String, Day2>();
       
        private String value;
        private Double num;
        public int samba = 1000;
        
     
        static{
        	dayMap().put("Open", DAY1STRING1);
        	dayMap().put("Closed",DAY1STRING2);
        	dayMap().put("Cancelled",DAY1STRING3);
        	dayMap().put("Reversed", DAY1STRING4);
        	dayMap().put("Reversal",DAY2DOUBLE1);
        	dayMap().put("Expired", DAY2DOUBLE2);
        }
       
        private Day2(String value){
        	this.value = value;
        } 
        
        private Day2(Double value){
        	this.num = value;
        }
    
        private static Map<String, Day2> dayMap(){
        	return dayMap;
        }
    }
    Thanks heaps,
    newbie :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    public static Map<String, Day2> dayMap = new TreeMap<String, Day2>();
    // ...
    private static Map<String, Day2> dayMap(){
        return dayMap;
    }

    The map is declared as static to indicate that it is an attribute of the whole enum not of the particular values. There is only one map which DAY1STRING1, DAY1STRING2, etc share. You don't have to make dayMap() shared, although it makes perfect sense to do.

    (I think you might have the visibility modifiers on these two backwards)

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi.
    Just Java has not that syntax. You can't make some operations outside methods or special constructions like static {}
    In that code is used static construction, because enum is immutable and web can full the map in static construction.
    In Effective Java is most detail to explain it.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 07:02 PM
  2. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 04:42 PM
  3. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 02:47 AM
  4. Enum example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:34 PM
  5. How to use enum
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08: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
  •