Abstract method in Enums

Did you know that you can do that? private static enum DynamicProperty { cacheManagerName { @Override void applyChange(final PropertyChangeEvent evt, final RuntimeCfg config) { config.cacheManagerName = (Str…

Did you know that you can do that?

private static enum DynamicProperty {

    cacheManagerName {
        @Override
        void applyChange(final PropertyChangeEvent evt, final RuntimeCfg config) {
            config.cacheManagerName = (String) evt.getNewValue();
        }
    },
    defaultCacheConfiguration {
        @Override
        void applyChange(final PropertyChangeEvent evt, final RuntimeCfg config) {
            LOG.debug("Default Cache Configuration has changed, previously created caches remain untouched");
        }
    };

    abstract void applyChange(PropertyChangeEvent evt, RuntimeCfg config);
}

I think it’s nice because it allows you to customize Enum’s behavior or perform other actions on use.

Piece of code taken from EhCache Configuration (line 118 and further).

You May Also Like

Use asInstanceOf[T] carefully!

BackgroundScala has nice static type checking engine but from time to time there are situations when we must downcast some general object. If this casting is not possible we expect that virtual machine will throw ClassCastExeption as fast as possible. ...