Consider I am having the following enum class,
public enum Sample {
READ,
WRITE
}
and in the following class I am trying to test th
I'm answering to the first question. You can easily do it like this:
public static void main(String[] args) {
Sample.READ.testEnumSample();
}
public enum Sample {
READ,
WRITE;
public void testEnumSample() {
System.out.println(this);
}
}
You cannot create a new enum instance. Otherwise it won't be an enum. You can reference an already existing enum. As in your example
Sample sample = Sample.READ;
Enums doesn't support public constructors and hence, cannot be instantiated. Enums are for when you have a fixed set of related constants. Exactly one instance will be created for each enum constant.