Question

i was using the class java.util.concurrent.TimeUnit and i found some interesting conventions about that i didn't knew and i didn't understand is about enumerations i have make a example on my own i hope somebody can explain to me..

a simple Interface with a method.

interface MyInterface{public String concate(String... a);}

latter i create a enumeration

public enum Enumerations implements MyInterface
{
   STRINGUTILITIES{},
   BEANENUMERATION{},   
   NANOSECONDS 
   {//sample methods
   public long toNanos(long d)   { return d; }
   public long toMicros(long d)  { return d; }
   public long toMillis(long d)  { return d; }
   public long toSeconds(long d) { return d; }
   public long toMinutes(long d) { return d; }
   public long toHours(long d)   { return d; }
   public long toDays(long d)    { return d;}
   public long convert(long d) { return d;}
   int excessNanos(long d, long m){return 13;}      
};  
@Override public String concate(String... a){throw new  AbstractMethodError();}                           

}

i need to implement the concate method is OK for me

my first question is.

STRINGUTILITIES{},
BEANENUMERATION{},   
NANOSECONDS{}

are enumerations? i think in fact are nested enumerations..

my main concern is if a comment the concate method on the outer enumeration Enumerations the

STRINGUTILITIES{},
BEANENUMERATION{},   
NANOSECONDS{}

complains they do not implement the method.. seems the 3 inner enumerations are in some way extending the outer enumeration and if the outer enumeration is implementing the method is OK for them.

like this

public enum Enumerations implements MyInterface
{
   STRINGUTILITIES{},
   BeanEnumeration{},   
   NANOSECONDS 
   {
    public long toNanos(long d)   { return d; }
    public long toMicros(long d)  { return d; }
    public long toMillis(long d)  { return d; }
    public long toSeconds(long d) { return d; }
    public long toMinutes(long d) { return d; }
    public long toHours(long d)   { return d; }
    public long toDays(long d)    { return d;}
    public long convert(long d) { return d;}
    int excessNanos(long d, long m){return 13;}
    };   
}

if the outer enumeration do not implement the method and i implement the method on each the nested enumerations is OK.

other question if comment the nested enumerations like this

interface MyInterface{public String concate(String... a);}
public enum Enumerations implements MyInterface
{   
    @Override public String concate(String... a){return "java";}                           
}

shows a error..

please somebody guide through explain how this approach is used and named..

thanks a lot..

Was it helpful?

Solution

Any object can have a non-static initialization block when declared:

Object a = new Object() {
    public long toNanos(long d) {
        return d;
    }
};

Enums are no exception.

The methods in the block have to override/implement methods in the class/interface of your reference type (Object above) in order to be callable. Notice that all of the methods in TimeUnit initialization blocks override methods in TimeUnit itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top