Why return and throw keywords are not allowed in a static block
A static
is for initializing variables static
, there should be no need to call return
or throw
a Exception
within one.
If you need to return
, or throw
a Exception
, I suggest using what he says JavaTutorial for static initialization block, and instead assign method:
class Whatever {
public static varType myVar = initializeClassVariable();
private static varType initializeClassVariable() {
// initialization code goes here
}
}
Taken from https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
source to share
A is return
not allowed because a static block is not strictly speaking a method. There is no "caller" to return, and of course nothing to return a value. In addition, you can always achieve the effect of local flow control of a return-with-no-expression by slightly modifying your code.
(In your example return
, it is overkill anyway ... from a thread-local control point of view.)
In short, return
not allowed because it doesn't make sense. (Just like you cannot use continue
in code that is not in a loop statement.)
Exceptions propagating from static blocks are problematic. Basically, the application code cannot catch and deal with them.
-
The whole element of a checked / unchecked distinction is to avoid ignoring some exceptions. Therefore, the Java language treats a static block propagating a checked exception as a compilation error.
-
On the other hand, you can throw a thrown exception in a static block (and not catch it). This leads to
ExceptionInInitializerError
and leaves the class you were trying to initialize in a dead state. Typically, the application cannot recover from this.Why is this allowed? Given the many ways in which an unchecked exception can be thrown, it would be impractical for the Java language to insist that they are considered inside a static initializer block. Also, unexpected Exception + Exception is an application error. The easiest / safest way to deal with a bug is to make the application die ... leaving evidence for the programmer to diagnose and fix the bug.
source to share
This question also applies to instance initializers as well as static initializers.
The operator return
can be allowed and will be just as useful as return
in a method void
. However, I think the biggest problem is the ambiguity in having multiple initializers:
static{ // 1
...
...return;
...
}
static{ // 2
...
}
if executed does return
just end the block 1
and keep blocking 2
, or does it terminate all static initialization code?
For the question, throw
see Why is it not allowed to throw an exception in the initialization block of a Java instance?
I understand that if a static / instance initializer throws an exception unconditionally, the class / constructor will always fail, making it almost useless. Therefore Java disallows it.
Workarounds:
return
can be modeled break
from a tagged block:
static
{
STATIC:
{
break STATIC;
}
}
Unconditional throw
can be achieved withif(true)
static
{
if(true) throw new Error();
}
(Java deliberately pretends not to know that the statement is definitely executed)
source to share