I'm writing some java code and at one moment I was blocked at one thing.
final String action = "action1";
final Button otherAction = (findById....);
otherAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (action.equals("action1")) {
action = "action2";
} else if (action.equals("action2")) {
action = "action3";
} else if (action.equals("action3")) {
action = "action1";
}
}
});
Obviously this code doesn't work because I can't assign a new value to action
, because it's a final variable and so can only be initialized one time.
And to access a variable from inside the onClickListener scope, you must declare it final.
So what I did to fix that is:
final Button otherAction = (findById....);
otherAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (t.action.equals("action1")) {
t.action = "action2";
} else if (t.action.equals("action2")) {
t.action = "action3";
} else if (t.action.equals("action3")) {
t.action = "action1";
}
}
});
Class t {
public static String action = "action1";
}
My question is: Why is this working?