You may have encountered this javac compiler error when trying to code something like this
public void work() {
final byte[] content;
try {
content = readContent();
} catch (IOException e) {
content = getDefaultContent();
}
// do stuff with content here
}What happens now is, that the compiler complains about the second assignment of content. If you are a fan of
The Final Story (Chapter 2 of
Hardcore Java) as big as we are, than getting rid of final is not
an option.
Do this instead:
private byte[] readContentIfPossible() {
try {
return readContent();
} catch (IOException e) {
return getDefaultContent();
}
}
public void work() {
final byte[] content = readContentIfPossible();
// do stuff with content here
}