I want to return from the static block.
Looks like the return and break statement don't work. Is there any alternative.
I know the bad workaround could be create a flag and check the flag to continue or not.
I understand that the initialisation blocks are not meant for doing computations but just for basic initialisation during class loading.
Delegate the code to a private static method:
Instead of using
return
just wrap your conditional code in anif
.You can not return from static block but better to use some other function that will perform your logic and return to the block.
You cannot return from a static initializer block. There is nowhere to return to. But it shouldn't be necessary. You should be able to restructure your code to be "single entry, single exit".
Static initialisers have no business being complicated, so it's probably a bad idea (even if you don't buy SESE).
The minimal way of achieving a return is to use a labelled break.
They are quite rare, typically appearing in nested
for
loops. The novelty might tip off the reader that something a bit dodgy is going on.