Answer the question
In order to leave comments, you need to log in
Is it possible to pass arguments to the enum constructor in this way?
Today in a Java SE class I ran into a small problem. I came across a syntax that was incomprehensible to me while reading interesting information about Enums on the site .
Let's start with what I already know:
values()
is very convenient to iterate thempublic enum Books {
DARK_TOWER {
@Override
public void info() {
System.out.println("Dark Tower, autor - Stephen King");
}
},
KTULHU {
@Override
public void info() {
System.out.println("Ktulhu, autor - Govard Lovecraft");
}
};
public abstract void info();
}
enum Type {
INT(true) {
public Object parse(String string) { return Integer.valueOf(string); }
},
INTEGER(false) {
public Object parse(String string) { return Integer.valueOf(string); }
},
STRING(false) {
public Object parse(String string) { return string; }
};
boolean primitive;
Type(boolean primitive) { this.primitive = primitive; }
public boolean isPrimitive() { return primitive; }
public abstract Object parse(String string);
}
true
the false
constructor(?) of Enum? Books b = Books.DARK_TOWER;
switch (b) {
case DARK_TOWER:
Books.DARK_TOWER.info();
break;
case KTULHU:
Books.KTULHU.info();
break;
}
case
`e I can't write like this: case Books.KTULHU: ...
?
Answer the question
In order to leave comments, you need to log in
You can consider this example:
public enum A {
AAA {
public void i() {
System.out.println("Hi AAA");
}
},
BBB {
public void j() {
System.out.println("Hi BBB");
}
},
CCC;
public static void main(String[] args) {
System.out.println(A.AAA.getClass().getName());
System.out.println(A.BBB.getClass().getName());
System.out.println(A.CCC.getClass().getName());
}
}
static final class A
extends com.home.A {
A(String string2, int n2) {
super(string, n, null);
}
public void i() {
System.out.println("Hi AAA");
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question