Why isn’t a final variable always a constant expression?

From the JLS

A blank final is a final variable whose declaration lacks an initializer.

A constant variable is a final variable of primitive type or type
String that is initialized with a constant expression (ยง15.28).

Your variable

final int a;

is a blank final variable. It lacks an initializer. The second paragraph doesn’t apply to it because it is not initialized at declaration. It therefore isn’t a constant expression.

This applies to fields as well.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)