0

I want to loop through enum values continuously. I can solve it outside the enum but I curious is there a way to solve it inside. Error occurs as NOON and EVENING not initialized yet.

@Getter
public enum TimeSlotEnum {

    MORNING(NOON, LocalTime.of(9, 0), LocalTime.of(12, 0)),//compile error
    NOON(EVENING, LocalTime.of(12, 0), LocalTime.of(15, 0)),//compile error
    EVENING(MORNING, LocalTime.of(15, 0), LocalTime.of(18, 30));

    private final TimeSlotEnum next;
    private final LocalTime start;
    private final LocalTime end;

    TimeSlotEnum(TimeSlotEnum next, LocalTime start, LocalTime end) {
        this.next = next;
        this.start = start;
        this.end = end;
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
Eduard Streltsov
  • 1,754
  • 20
  • 19
  • What do you intend to do with this enum? Can you give an example? We can surely make the compilation error go, but it depends on what you want to achieve :) – aksappy Oct 09 '19 at 17:08
  • Sorry for duplicated question. Didn't find the answer by my requests. Just solved it. public enum TimeSlotEnum { MORNING, NOON, EVENING; private TimeSlotEnum next; private LocalTime from; private LocalTime till; static { MORNING.next = NOON; MORNING.from = LocalTime.of(9,0); MORNING.till = LocalTime.of(12,0); NOON.next = EVENING; NOON.from = ... ... } – Eduard Streltsov Oct 09 '19 at 17:18
  • 1
    @EduardStreltsov: if you found the answer yourself, it's perfectly acceptable and in fact encouraged to just post an answer to your own question. Here in the comments it's likely to be missed by anyone else looking for the same thing in the future. – Joachim Sauer May 12 '23 at 10:27

0 Answers0