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]; } } 
5
  • 2
    Which line gives the error? Commented Sep 29, 2013 at 22:45
  • Please show the line of code that causes the error. Commented Sep 29, 2013 at 22:50
  • @hexafraction: It doesn't seem that way to me. He's seems to be asking because he doesn't think he has any static code, not because he's wondering why he can't call non-static code from static code. Commented Sep 30, 2013 at 0:04
  • @Dolda2000 floor.changeTileColor(row, col); seems static enough to me. Simialr enough for the CV IMHO. Commented Sep 30, 2013 at 0:04
  • 1
    @hexafraction: I'm not saying he was right, I was just saying that was his intention with asking. :) Commented Sep 30, 2013 at 0:06

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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).

1 Comment

Right answer! Also, OP should change the name of the class from floor to Floor to conform with Java naming standards.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.