Why can't I lazily initialize a static long array like this in java?

private static final long[] reservedFromIps; static { reservedFromIps = {0l, 167772160l, 1681915904l, 2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; } 

A mistake is "the illegitimate beginning of the expression, not the statement expected"

while the following works just fine:

 private static final long[] reservedFromIps = {0l, 167772160l, 1681915904l, 2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; 
+4
source share
4 answers

This has nothing to do with static blocks; array constants can only be used in initializers. This is exactly how the language is indicated.
This code will not compile:

 public class Test { public static void main(String[] args) { long[] reservedFromIps; reservedFromIps = {0l, 167772160l, 1681915904l, 2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; } } 

Why this is so is probably due to the extra complexity for the compiler with a little added winnings, but to be sure you have to do this with the Java development team.

+5
source

Firstly, there is a typo in your static initializer block (or in the field declaration). Secondly, you will need to do this:

 static { reservedFromIps = new long[]{0l, 167772160l, 1681915904l, 2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; } 

Array constants can only be used in initializers, and not when reassigning an array.

+3
source

The Java compiler cannot decide the type of your abbreviated expression. The syntax requires a type identifier before an expression, regardless of whether it is an expression of a type. An exception is made only for primitives and strings. The array is not primitive.

When used as an initializer, the type of the innitializer expression is already known - the same as the initialized variable type.

+1
source

An array type variable declaration must go with initialization in the same expression. Like long[] reservedFromIps = { /* ... */} . This is required by syntactic sugar (with which we can only initialize the array with {} ).

eg. the following should work:

 static { reservedFromIps = new long[] {0l, 167772160l, 1681915904l, 2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; } 

Reason why the following

 private static final long[] reservedFromIps = {0l, 167772160l, 1681915904l, 2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; 

works because in this case the declaration and initialization of the reservedFromIps array are in the same expression.

0
source

Source: https://habr.com/ru/post/1434947/


All Articles