I use a static enum in an interface and want to use it in an extending class.
I have following interfaces:
public interface StateSupport {
public static enum State {
NEW,
UNCHANGED,
UPDATED;
}
}
and
public interface Support extends StateSupport {
public void do(Context arg0);
}
and finally a class
public class MyClassUtil implements Support {
public void do(Context arg0){
MyClass obj = new MyClass(NEW);
}
}
The point is that I dont want to write "State.NEW", just "NEW" :-)
So how can it do that without using the enum name. Is there a way at all?
Enumerated Type Declaration to Create a Variable Similar to pre-defined data types like int and char, you can also declare a variable for enum and other user-defined data types. Here's how to create a variable for enum.
Since you cannot override valueOf method you have to define a custom method ( getEnum in the sample code below) which returns the value that you need and change your client to use this method instead. getEnum could be shortened if you do the following: v. getValue().
You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.
Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.
You can use a static import:
import static com.yourpackage.StateSupport.State.NEW;
import static com.yourpackage.StateSupport.State.UNCHANGED;
import static com.yourpackage.StateSupport.State.UPDATED;
or in short (discouraged):
import static com.yourpackage.StateSupport.State.*;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With