4
private static final long[] reservedFromIps;

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

The error is "illegal start of an expression, not a statement, ; expected"

whereas the following works fine :

private static final long[] reservedFromIps = {0l, 167772160l, 1681915904l, 
    2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l, 
    3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l}; 
user1364534
  • 105
  • 5
  • possible duplicate of [Why can't you use shorthand array initialization of fields in Java constructors?](http://stackoverflow.com/questions/8302179/why-cant-you-use-shorthand-array-initialization-of-fields-in-java-constructors) – kosa Sep 18 '12 at 21:35
  • yeah, sorry about that. I checked most of the suggestions and did a search myself but did not see that one. – user1364534 Sep 18 '12 at 21:42
  • May be lazy-initialize mis-lead the search. – kosa Sep 18 '12 at 21:43
  • Sorry I am not a native speaker, did I misspell? Feel free to change the title or delete the question, assuming you are able to. – user1364534 Sep 18 '12 at 21:46

4 Answers4

5

This has nothing to do with static blocks, array constants can only be used in initializers. It's just how the language is specified.
This code wont compile either:

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 the case is probably a question of added complexity for the compiler with little added gain, but to be entirely sure you would have to take it up with the Java design team.

Keppil
  • 45,603
  • 8
  • 97
  • 119
3

Firstly there's a typo in your static initializer block (or in your field declaration). Secondly you'll have 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.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • typo was me being lazy-initialized, first I wanted to write an example then changed my mind and copy-pasted the rest. Thanks, btw, your answer works. – user1364534 Sep 18 '12 at 21:39
  • Yea I figured the typo was from something like that :-P Anyway, glad I could help! – arshajii Sep 18 '12 at 21:42
1

Java compiler cannot resolve type of your shorthand expression. Syntax requires type identifier before expression, no matter what is that expressions, type. An exception is made only for primitives and string. Array is not a primitive.

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

Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29
0

The declaration of the array type variable must go with the initialization in the same statement. Like long[] reservedFromIps = { /* ... */}. It's required by the syntactic sugar (by which we can initialize an array with just {}).

e.g. the following should work -

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

The reason that the following

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

works is because in that case, the declaration and initialization of the array reservedFromIps are in the same statement.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142