@w1024020103
2017-01-24T16:58:04.000000Z
字数 1211
阅读 746
Java
Eclipse
static
non-static
reference
I have member variables like this:
public class Solver {
private List<Board> solutionBoards = new ArrayList<>();
private boolean solved;
later I have codes like this:
public boolean isSolvable(){
return solved;
}
public int moves(){
int moves;
if(isSolvable()) {
moves = solutionBoards.size() - 1;
}
else {
moves = -1;
}
return moves;
}
Eclispse says:
Cannot make a static reference to the non-static field solved
Cannot make a static reference to the non-static field solutionBoards
It offers me quick fix available:
Change 'solved' to 'static';
Change 'solutionBoards' to 'static'.
actually, I have codes like this:
private static class SearchNode{
private int moves;
private Board board;
private SearchNode prevNode;
private SearchNode(Board board, int moves, SearchNode prevNode){
this.board = board;
this.moves = moves;
this.prevNode = prevNode;
}
public int getMoves() {
return moves;
}
public int getPriority(){
return board.manhattan()+ moves;
}
public Board getBoard(){
return board;
}
public SearchNode getPreviousNode(){
return prevNode;
}
Finally, I saw 'static'!
I guess SearchNode class has been somehow used by those two methods below, so that's where the 'static' issue comes. I deleted the static here and there's no error anymore. I haven't really thoroughly understand the static thing yet.