I can create an empty Java enum type, but when i want to add some methods : i get a "syntax error" (into Eclipse). I found no official documentation about this, so my question is : is it just impossible (so where is it clearly mentioned?) or is it just the compiler which is wrong ?
An ENUM can also contain NULL and empty values. If the ENUM column is declared to permit NULL values, NULL becomes a valid value, as well as the default value (see below).
The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.
An enum class can include methods and fields just like regular classes. When we create an enum class, the compiler will create instances (objects) of each enum constants. Also, all enum constant is always public static final by default.
setCode (Main. Code. EMPTY); your variable 'code' of the 'Main' instance would be an empty string.
Yes it's possible. You just need to add a ;
to terminate the (empty) list of enum constants:
enum TestEnum {
; // doesn't compile without this.
public void hello() {
System.out.println("Hello World");
}
}
JLS Syntax Definition for enum
s
(Note that without any instances, you'll only be able to call the static
methods of the Enum.)
Related:
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