简体   繁体   中英

Can someone explain to me why I'm having this error?

Good day,

I have a question on which in my code, one declaration is having an error outside a function.

The snippet is:

public class Gold {

    Block[][] block = new Block[4][4];


        this.block[1][4] = new Block(); //1


        public void populate() {
            this.block[1][4] = new Block();//2
    }

}

The code this.block[1][4] = new Block(); number 2 is not having an error. However, the first code which is not inside the function is showing the error:

Illegal start type

Cannot find symbol

']' expected

invalid method declaration, return type required.

I just want to understand why this is hapenning and what I am missing.

Thanks

没有索引为4的元素。最后一个元素的索引为3。

You can not initialize variables directly as you did above in case 1, You can initialize block[][] in constructor like :

public Gold(){
    this.block[1][3] = new Block();
}

OR in block like :

Block[][] block = new Block[4][4];
{
      this.block[1][3] = new Block();
}

You have a statement which must be in a block of code.

Try

public class Gold {
    Block[][] block = new Block[4][4];
    { // initializer block
        this.block[1][3] = new Block(); //1
    }

You have two problems here, You need to initialize any object in a scope. Either you have to call the Number 1 line in a constructor or in a function or with in a block (enclosed with in { and } ) . Here you need to know difference between static block and non-static blocks .

Other problem you have is this.block[1][4] = new Block(); will throw an ArrayIndexOutofBounds exception as the length of the array is 4 and you are trying to insert and object at 5th place.

I think that you should place your code in constructor:

public Gold(){
    this.block[1][4] = new Block();
}

In java class you can define variables or methods (including constructors) but can not use plain code in it.

You need to wrap your code in a method, not in the class body. If its part of the initialization of the Gold class put it inside its constructor like:

public class Gold {

    Block[][] block = new Block[4][4];

    public Gold() {
          this.block[1][4] = new Block();
    }

}

The class body allows only variable declaration and instantiations, but not standard operations like changing an object's (your array) value.

EDIT: as other pointed out, the index 4 does not exist in an array of size 4 , only indices from 0 to 3 can be accessed.

this.block[1][4] = new Block(); //1

is an expression not a field initialization. It cannot be outside of a method body.

This sentence

this.block[1][4] = new Block(); //1

cannot go outside a method or Initialisation block. If you want to initialize the array you have to do it on the declaration or inside a method (constructor could be a good place) or you have to use a Initialisation block.

Also check the array bounds. In Java arrays have 0 based indexes.

The code this.block[1][4] = new Block() is compiled but will fail on runtime: your array has 4 elements while index in java and all C-like languages starts from 0, so the max value of 4-elements long array is 3.

And you cannot write code outside methods in java, so if you try this you get compilation error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM