不能从静态上下文中引用非静态方法 (Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19084146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-static method cannot be referenced from a static context (Java)
提问by Tessa Olowniuk
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.
我是编程新手,并且在从我的 ant 类调用我的 floor 类时,一直无法从静态上下文中引用非静态方法。我已经删除了所有静态,但仍然出现此错误,如果有人能指出我正确的方向或让我知道问题出在哪里,那就太好了,谢谢。
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];
}
}
回答by Jeanne Boyarsky
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;
由于其他原因,该代码无法编译。例如,在此构造函数中,未定义 startDir。虽然定义了大小,但没有任何作用。它将参数大小分配给自己。你真的想要 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.
此外,行和列未在任何地方定义。如果您收到有关静态的错误,我想知道您是否正在编译旧代码。
回答by clwhisk
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.
public static void main()
是一个静态上下文。它只有一个,而从技术上讲makeMoves()
,每个Ant
对象都有一个。在 的实例上调用它们Ant
,或者static
也创建它们。这就是要点,只需查找关键字static
和/或上下文,范围即可了解更多信息。
回答by Dolda2000
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.
但是,我认为您的基本问题(至少因为它认为所提出的问题)是您认为您已经在Ant
类中为地板定义了一个实例变量,而实际上您没有。您刚刚在Ant
构造函数中为它定义了一个局部变量,一旦构造函数返回,它就会被丢弃。
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.
然后,由于您在floor
类本身和您可能认为将保持 Ant 的 floor 的变量之间存在命名冲突,因此您尝试调用floor.changeTileColor
,认为它将在 Ant 的 floor 实例上被调用,但它编译好像它是对静态方法的引用。这是因为floor
,这里指的是类floor
本身,而不是指向它的实例的变量。
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).
要解决它,请floor
在Ant
类中创建一个实例变量,而不仅仅是在构造函数中(不过,建议使用另一个名称,以避免更多的命名冲突)。