Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31612 > unrolled thread

Re: enum, what is it good for?

Started byRobert Klemme <shortcutter@googlemail.com>
First post2015-04-24 20:28 +0200
Last post2015-04-25 10:41 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: enum, what is it good for? Robert Klemme <shortcutter@googlemail.com> - 2015-04-24 20:28 +0200
    Re: enum, what is it good for? Sebastian <news@seyweiler.dyndns.org> - 2015-04-24 22:49 +0200
      Re: enum, what is it good for? Robert Klemme <shortcutter@googlemail.com> - 2015-04-25 10:41 +0200

#31612 — Re: enum, what is it good for?

FromRobert Klemme <shortcutter@googlemail.com>
Date2015-04-24 20:28 +0200
SubjectRe: enum, what is it good for?
Message-ID<cpvgbcFaqjeU1@mid.individual.net>
On 21.04.2015 04:30, Stefan Ram wrote:

>    So, is there any reason to prefer »enum« to »class«?

Additionally to what others have said in favor of enums already, you can 
*very* concisely override methods in enum instances which is helpful for 
implementing state, strategy and similar patterns.  With classes that 
will look a lot more bulky.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [next] | [standalone]


#31613

FromSebastian <news@seyweiler.dyndns.org>
Date2015-04-24 22:49 +0200
Message-ID<mhea9b$j9u$1@news.albasani.net>
In reply to#31612
Am 24.04.2015 20:28, schrieb Robert Klemme:
> On 21.04.2015 04:30, Stefan Ram wrote:
>
>>    So, is there any reason to prefer »enum« to »class«?
>
> Additionally to what others have said in favor of enums already, you can
> *very* concisely override methods in enum instances which is helpful for
> implementing state, strategy and similar patterns.  With classes that
> will look a lot more bulky.
>
> Kind regards
>
>      robert
>
can you elaborate on that? In what way is overriding an Enum method more 
concise than overriding an ordinary class method?

In any case, I am delighted that it is not too late to discuss Java 5 
features. Here's a short Enum-related blog post from May 2008 which I 
have always liked:

http://tobega.blogspot.de/2008/05/beautiful-enums.html

Too bad we have lambdas now.

-- Sebastian



[toc] | [prev] | [next] | [standalone]


#31614

FromRobert Klemme <shortcutter@googlemail.com>
Date2015-04-25 10:41 +0200
Message-ID<cq129jFma4lU1@mid.individual.net>
In reply to#31613
On 24.04.2015 22:49, Sebastian wrote:
> Am 24.04.2015 20:28, schrieb Robert Klemme:

>> Additionally to what others have said in favor of enums already, you can
>> *very* concisely override methods in enum instances which is helpful for
>> implementing state, strategy and similar patterns.  With classes that
>> will look a lot more bulky.

> can you elaborate on that? In what way is overriding an Enum method more
> concise than overriding an ordinary class method?

package enm;

public enum Fmt1 {
   BRACKET {
     @Override
     public String format(Object o) {
       return "(" + o + ")";
     }
   },
   BRACES {
     @Override
     public String format(Object o) {
       return "{" + o + "}";
     }
   },
   SQUARES {
     @Override
     public String format(Object o) {
       return "[" + o + "]";
     }
   };

   public abstract String format(final Object o);

}

vs.

package enm;

public abstract class Fmt2 {

   public static final Fmt2 BRACKET = new Fmt2() {
     @Override
     public String format(Object o) {
       return "(" + o + ")";
     }
   };

   public static final Fmt2 BRACES = new Fmt2() {
     @Override
     public String format(Object o) {
       return "{" + o + "}";
     }
   };

   public static final Fmt2 SQUARES = new Fmt2() {
     @Override
     public String format(Object o) {
       return "[" + o + "]";
     }
   };

   public abstract String format(final Object o);
}

Granted, the difference is not too big.  If you take all differences 
together (i.e. the boilerplate that you have to write to get all the 
other features mentioned) it does make a significant difference.

Kind regards

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web