Reputation: 11098
I recently ran into this error that took me quite a while to figure out.
I thought I should post it on here in case someone else might run into it too, though the probability of that might be extremely low (see below).
I recently started using enum in Java, as follows
public enum State {
ON, OFF
}
And then, in my object, which extends from Thread
, there is a variable called state:
public class Example extends Thread {
private State state;
public Example() { state = State.OFF; } // initialize object at OFF state
This will however gives an error at compile time as such:
./Example.java:3: error: cannot find symbol
state = State.OFF;
^
symbol: variable OFF
location: class State
1 error
Upvotes: 3
Views: 4557
Reputation: 11098
After struggling with this for about an hour, I figured out the problem.
There is in fact a class name State
that is a part of the Thread
object. Therefore, when I call State.OFF
, the compiler is looking for this state, which cannot be found.
You can figure this out by trying to take off the extends Thread
part on the class declaration, and it will run fine. If you change the name of the enum
type from State
to something else, for eg. States
, that would be fine too. So you will only run into this problem when extending from Thread and using State as the name.
Just as an FYI, you can find out all the different states for the Thread object by doing:
for (State s : State.values() {
System.out.println(s);
}
You will get something like:
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
Pretty interesting for a rookie like me I must say.
Upvotes: 2