Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: enum, what is it good for? Date: Sat, 25 Apr 2015 10:41:22 +0200 Lines: 79 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net awyDR6ZxCe/NYtugPpTdIgsrnj13SMC31RmeOHgHtILiTgdQI= Cancel-Lock: sha1:JvssS7yzPPUYxcXqSBZ8o8/R3gs= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:31614 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/