1

Here's what I've written:

public class JavaApplication4 {
    private RunMode runMode;

    private enum RunMode {
        STOP, START, SCE, SIE;

        void reset() {
            this = STOP; // <=== 'cannot assign a value to final variable this.'
        }
    }

}

As noted, the assignment to 'this' is flagged. Why is 'this' final, and how can I change the value of an enum variable with an enum instance method?

Chap
  • 3,649
  • 2
  • 46
  • 84
  • 1
    why do you want to do that - that is the purpose – Petar Ivanov Aug 02 '11 at 06:43
  • I did not grasp the fact that each value of an **enum** is a constant, "pre-instantiated" object, independent of whether a reference to it has been assigned to a variable. I guess I was confusing the variable (which I wanted to change) with the *constant* object to which it currently referred. In retrospect, all I really wanted was for my RunMode enum definition to include a specification of the "default" RunMode; namely, STOP. – Chap Aug 02 '11 at 08:12

5 Answers5

2

It really doesnt make sense to reassign an already instantiated Enum. Think of Enums as singleton objects. In your case, START, STOP, SCE and SIE are all singleton objects that are pre-instantiated. All you do is pass around their reference in your application.

arun_suresh
  • 2,875
  • 20
  • 20
  • 2
    All answers helped, but this one really got to the heart of my misunderstanding. Enum values are pre-instantiated, constant, singleton objects. When I set foo=RunType.STOP, and then want to change foo, I must assign something else to foo, *not* change the constant that foo is currently bound to! – Chap Aug 02 '11 at 07:53
2

The enum exists to provide you with a set of related constants that are used to describe some state (in your case, the run mode of the application). The instances are immutable, for good reason: they are supposed to represent constants.

You don't really want to "reset" the object that represents a run mode. You want to reset the run mode of the application. So the functionality belongs in the application class, and it is implemented by assigning a different enum object to the field.

public class JavaApplication4 {
    private RunMode runMode;

    public void reset() {
        runMode = RunMode.STOP;
    }

    private enum RunMode {
        STOP, START, SCE, SIE;
    }
}
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1

You can never change the value of an enum from one to another. Enums are meant to be constants. It sounds like you should probably return STOP from the reset() method (and any other potentially-state-changing methods) and write:

runMode = runMode.reset(); // etc

It's important to understand that enums are reference types, and meant to effectively be a collection of constant values. Not only can you not mutate one value into another, but you shouldn't change the value of any fields within the enum either (unless it's for caching).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • To avoid confusion, let's call the variable 'rm'. Would it be `rm = rm.reset()`, or `rm = RunMode.reset()`? I guess both would work, but both seem overly wordy. I'd wanted to be able to write `rm.reset()` but I guess I can't, if rm is simply an enum type. – Chap Aug 02 '11 at 08:25
  • @Chap: It sounds like you may want a mutable type *around* your `RunMode` - as per Karl's answer. – Jon Skeet Aug 02 '11 at 08:39
  • Quite so. Altogether an enlightening discussion! – Chap Aug 02 '11 at 19:49
1

Enums are just a bunch of constants grouped together under one type.

Is that what you want:

private RunMode runMode;

private enum RunMode {
    STOP, START, SCE, SIE;
}

void reset() {
    runMode = RunMode.STOP;
}
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
1

this is not a modifiable entity. Easiest way is to move your reset() inside the class body itself:

public class JavaApplication4 {
    private RunMode runmode;

    private enum RunMode {
        STOP, START, SCE, SIE;
    }
    void reset() {  // <---- move it here
        runMode = RunMode.STOP; // <=== ok
    }
}
iammilind
  • 68,093
  • 33
  • 169
  • 336