Greg Greg - 7 months ago 14
Java Question

Java, operations with an array of objects

I want to store the player's data and the field's data in two arrays of object. The problem is I need to work with these in a different classes. In the class where I originally created its not a problem but when I try to call it in another class it throws "

board[i]
cannot be resolved to a variable".

The code I am writing is kind of similar to this:

class Field {
int something;
int another;

public void setMethod(int something){
this.something = something;
}

//etc.
}




class Board {
public static Field[] board = new Field[60];

public void BoardFiller(){
for(int i = 0; i < board.length; i++){
board[i] = new Field(/*construct*/);
//etc.
}
}
}




class Gameplay {
public boolean Attackable() {
boolean atk = false;

//stuff
int something = board[i].getMethod();
//more stuff
return atk;
}
}


And so on. I have to use the Field data to check if it's reachable, use it in the turns, in the I/O part.
The problem is the same with the Player data.

Answer

Step 1: Remove the static modifier from the board variable

Step 2: Add a public Board board = new Board(); field to the Gameplay class

Step 3: Use board.board[...] to access the fields in the Gameplay class