I don’t know if what I want to do is even doable but I’ll ask anyway.
I want to find out which enum value is being constructed in this enum’s constructor.
Here is the pseudo code:
enum SomeEnum {
VALUE_ONE, VALUE_TWO;
private SomeEnum() {
System.out.print(*some-reflection-magic*);
}
}
So when I call SomeEnum.VALUE_ONE somewhere else I’ll get “VALUE_ONE” on my System.out.
Don’t ask why, it’s just for fun 🙂
If you want something printed out every time an enum constant is used, then the answer is no. Enum values are instantiated once (and in many ways constitute the quintessential singleton implementation on the JVM), typically when the
enumtype is loaded. This is why, when printing inside theenum‘s constructor, you’ll observe that all the values are being instantiated.