Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
You have to reference your static variable via the class it is defined in. In other words: Board.board[i] – Mark B Apr 16 '16 at 16:34
up vote 0 down vote accepted

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

share|improve this answer
    
"Step 0" add a method to GamePlay, as is it's invalid – RC. Apr 16 '16 at 16:35
    
Thank you, solved now working just fine. – Greg Apr 16 '16 at 16:47
    
k this method works too. i just asked him to make the filed class static too. bt this way is right since he is calling methods from field class – Priyamal Apr 16 '16 at 16:49

Just like you created a field object using new Field() you need to create a board object in your gameplay class to be able to use the methods in there.

Board myBoard = new Board();
share|improve this answer

You have not defined the board variable in the scope of the Attackable method or the Gameplay class. In order to get access to the board variable, you have to access it by doing Field.Board.board. instead of just board.

share|improve this answer

In GamePlay class you have to loop over the board. Something like for(Field f : new Board().board) //do something

share|improve this answer

the code is totally wrong it wont even compile you are not allowed to create static array of outer class within the inner class

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields

you should get a compile error you can fix this by making your field class static.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.