Using enum, how does this work?

I have heard about enums before but I have never used them. I thought they were kind of like structs in C but now I see a whole lot of different things that confuse me.
I didn’t have too much trouble with the idea of methods inside an enum but I have a question regarding a certain functionality I read about. Here’s a piece of example code.


public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

Now, this means I can declare a Day variable as any of these values. But I’ve read that you could also somehow declare a value that will “always be true”. It will return true regardless of the comparison. Let’s say we call that value DAY and we declare

x = Day.DAY;

then we should get true from

x == Day.FRIDAY;

How exactly does this work? How do you declare that sort of value in an enum?

Hey man i use this in its simple form like you have showen to see what should be on the screen currently (Menu, Game, Inventory, etc) but i also have more complex examples where i have made it easy to make a new item for my game and one line of code later i can use it


public enum CurrentScreen {
	MAIN_MENU(),GAME(),INVENTORY(),INGAME_MENU(),MAP(),CHAT();
}

But if you want to store more complex data like items, tiles etc you need to do something like this


public enum Item {
	Starter_Bow("Starter Bow",0,0,"You will need this adventurer",ItemType.Combat);
	protected String name;
	protected int x;
	protected int y;
	protected String description;
	protected ItemType type;
	Item(String name,int x, int y, String description,ItemType type){
		this.name = name;
		this.x =x;
		this.y = y;
		this.description = description;
		this.type = type;
	}
	public String getName(){
		return name;
	}
	public String getDescription(){
		return description;
	}
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}
	public ItemType getItemType(){
		return type;
	}
}

and to pull data from it you would do;
System.out.println(Item.Starter_Bow.getName());

or
Item item = Item.Starter_Bow;
item.getName();

The best way i could explain it is like creating a new instance of the class for all items you require and you pull the instance of the class with Item.[name].[sub function]

To my knowledge there isn’t an enum value that will equal any other value of that enum but you can use instanceof to test if the enum is of that type.


Day x = Day.MONDAY;

System.out.println(x instanceof Day); // prints true

Can you explain a little more about what you are trying to accomplish?

Um, I think I might have been misunderstood.
I mean something like


/** Types of foobangs. */
public enum FB_TYPE {
 GREEN, WRINKLED, SWEET, 
 /** special type for all types combined */
 ALL;
}

This is the code that has me confused. So, that ALL works for all the three previous or what? Does FB_TYPE.ALL == FB_TYPE.GREEN result in a true or a false?

False without special comparison.

Ok so I head that you have to use instanceof, fine. So, does this become true or false?


FB_TYPE.ALL instanceof FB_TYPE.GREEN

instanceof would be false in your snippet.

Example where instanceof would be true:

public interface OBJ {
}
public class Another implements OBJ {
}

In that example any instance of [icode]Another[/icode] would result in true if you use [icode]instanceof OBJ[/icode]

What are you trying to accomplish with this special value that equals all other values of the same enum?

Not really trying to accomplish anything. I just saw that code and I’m confused because I can’t understand what they mean with that comment

Ok (nothing wrong with experimenting!). I don’t think it can work that way. You would have to write a custom comparison method. Note, you cannot override the standard equals method as it is final. You could do something like:


public enum Day {

    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, DAY;

    public boolean isEqualTo(Day otherDay) {
        return otherDay == this || otherDay == DAY || this == DAY;
    }
    
}


// Then later
Day x = Day.MONDAY;

System.out.println(x.isEqualTo(Day.DAY)) // prints true

It looks like you’re thinking of bitflags, but have it all garbled up.

In C, it’s a common idiom to use the bitwise OR operator to munge several enum values together, then test them with the bitwise AND. You can then declare a single enum value that ORs everything together for you. Like this:


enum MyFlags {
    NONE = 0,
    GREEN = 0x01,
    WRINKLED = 0x02,
    SWEET = 0x04,
    ANOTHER = 0x08,
    ALL = GREEN | WRINKLED | SWEET
};

A comparison with ALL will only return true if it is actually compared with ALL. You can’t get true from ALL == GREEN, for example. To test, you have to use AND: if( val & GREEN ) == GREEN)…

AFAIK, the way to do this in Java is either to give your enum a constructor that takes an int parameter (since the values can’t be sequential - you have to use sequential powers-of-two) or to use an EnumSet. But I don’t know of any magic wildcard enum trick.

Yes, use EnumSet, which is already backed by bitflags underneath.

eg.


EnumSet<Day> mwf = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);
EnumSet<Day> weekdays = EnumSet.range(Day.MONDAY, Day.FRIDAY);
EnumSet<Day> all = EnumSet.allOf(Day.class);

mwf.contains(Day.TUESDAY); // false
weekdays.contains(Day.TUESDAY); // true
all.contains(Day.TUESDAY); // true