![]() ![]() Renaming Abhin's terms, this would be how I would apply it (similar to Abhin's example): value="10"), However, I document here, in the form of an answer to my own question, what I actually did. I chose Abhin's as the answer to my question because it was the most comprehensive and it worked when I tried it out. It will be better to create subclasses for the different restriction types. However, I'm concerned for the lack of OO here - if the restrictions have different behaviour or data needed for definition, then you will end up lumping everything in the Restrictions class. Which is then used exactly as your original code: String userName Static public Restriction setValue(int value) This.value = value // you don't really need Private Restriction(RestrictionType type, int value) ![]() Private Restriction(RestrictionType type) Static public final Restriction maximumLength = new Restriction(RestrictionType.maximumLength) Static public final Restriction enumeration = new Restriction(RestrictionType.enumeration) Static public final Restriction none = new Restriction(RestrictionType.none) If you make Restriction a regular class, with private constructor and static constant fields, you can then use method chaining to create new instances fluently: enum RestrictionType You can achieve what you want, but not with enums directly. Is there a way to accomplish what I wish to accomplish To which, the compiler croaks The value for annotation enum attribute must be an enum constant expression. So that, I intended to do this: String userName Restrictions restrictions() default Restrictions.none However, my actual motivation is to use that restriction in an ElementType.FIELD, ElementType.METHOD}) ![]() The reason being is, I am using enum to restrict the type of restriction that could be used, and be able to assign a value to that restriction. So that I could happily do something like this, which is perfectly legal syntax. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |