I have a variable which must be final
for a reason. But while trying to assign a value to it, an Exception
may be thrown in such case I need it to be 0. So I tried something like this.
private static final int x;
static {
try {
x = (1 / 0);
} catch (Exception e) {
x = 0; //error line
}
}
Surprisingly it gives me a compile-time error saying,
variable
x
might already have been assigned.
I cannot understand this, x
can only be assigned in try
block OR in the catch
block, possibly where else can a value be assigned to x
? and how can I overcome this problem?
EDIT :