0

I'm newer to programming and keep getting the non-static method cannot be referenced from a static context when calling my floor class from my ant class. I've removed all statics and am still getting this error, if someone could point me in the right direction or let me know what the issue is that would be great, thanks.

public class Ant {

    public final int RED = 0, BLUE = 1, NORTH = 0, 
            EAST = 1, SOUTH = 2, WEST = 3;
    public int color;

    public Ant(int size, int dir) {  
        size = size;
        dir = startDir;
        floor floor = new floor(size);
    }

    public int getRow() {
       return row; 
    }

    public int getCol() {
        return col;
    }

    public void makeMoves(int numMoves, int dir) {
        final int[][] offSet = {/* NORTH  */ {-1, 0},
                               /* EAST   */  {0, 1},
                               /* SOUTH  */  {1, 0},
                               /* WEST   */  {0,-1}};

        final int[][] newDir = {/* NORTH  */ {WEST, EAST},
                               /* EAST   */  {NORTH, SOUTH},
                               /* SOUTH  */  {EAST, WEST},
                               /* WEST   */  {SOUTH, NORTH}};
        //set start row, col, and direction
        row = col = size/2;

        for(int move = 1; move <= numMoves; move ++) {
            //make a move based on direction
            row = row + offSet[dir][0];
            col = col + offSet[dir][1];

            //turn based on color of new tile and direction
            color = floor.getTileColor(row, col);
            dir = newDir[dir][color];

            //change color of current tile
            floor.changeTileColor(row, col);
        }      
    }//End of makeMoves
}//End Ant class

public class floor {    
    int [][] grid;

    public floor(int size) {
        grid = new int[size][size];
    }

    public int getTileColor(int row, int col) {
        return grid[row][col];
    }

    public void changeTileColor(int row, int col) {
        int color = grid[row][col];
    }
}
Andres F.
  • 403
  • 1
  • 13
  • 24
Tessa Olowniuk
  • 1
  • 1
  • 1
  • 1

3 Answers3

1

That code doesn't compile for other reasons. For example, in this constructor, startDir isn't defined. And while size is defined, that doesn't do anything. It assigns the parameter size to itself. You really want this.size = size;

public Ant(int size, int dir)
    {  
        size = size;
        dir = startDir;

Also, row and col aren't defined anywhere. If you are getting errors about statics, I wonder if you are compiling old code.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
1

public static void main() is a static context. There's only one of it, whereas there is technically one of makeMoves(), say, for each Ant object. Call them on an instance of Ant, or make them static also. That's the gist, simply look up keyword static, and/or context, scope to learn more.

clwhisk
  • 1,805
  • 1
  • 18
  • 17
1

As others have pointed out, the code you're compiling doesn't seem to match the code you posted, because the posted code contains other errors than just static accesses.

However, I think your basic problem (as it regards the question as posed, at least) is that you think you have defined an instance variable in the Ant class for the floor, while in fact you have not. You have just defined a local variable for it in the Ant constructor, which is discarded as soon as the constructor returns.

Then, since you have a naming conflict between the floor class itself and what you probably think is the variable that would hold the Ant's floor, you're trying to call floor.changeTileColor, thinking that it would be invoked on the Ant's instance of floor, but it compiles as if it were a reference to a static method. This is because floor, here, refers to the class floor itself rather than to a variable holding an instance to it.

To solve it, create a floor instance variable in the Ant class instead of just in the constructor (though, suggestedly, under another name, in order to avoid more naming conflicts).

Dolda2000
  • 25,216
  • 4
  • 51
  • 92