A non-static variable that cannot be referenced from a static context

The error from this line is BoardState addme = new BoardState ();

For some reason, the non-static variable that he points to is "new." I do not understand how I can fix this error, since the new one is not intended for the variable, and it is not.

When looking at stackoverflow entries, this error usually comes from a non-static method, which is usually solved by a static method or bypassing the method completely. T

This code below contains a link to what happens before and after this statement.

public class IntelligentTicTacToe extends TicTacToe { public class BoardState{ public String TTTState; public int[][] defensiveOppsArray; public int[][] offensiveOppsArray; public String str; public int cnt; } public static ArrayList<BoardState> memory = new ArrayList<BoardState>(); public static boolean makeMove(){ char[] oArray = new char[TicTacToeArray.length]; int[][] defensiveOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length]; int[][] offensiveOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length]; int[][] sumOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length]; //converts our Array into a String String x = convertTTTArrayToString(); //Goes through the conditions to see if we have it in memory or if we must go through all the conditions boolean matchFound = false; for(int i=0; i < memory.size(); i++){ BoardState element = memory.get(i); if(element.str.equals(x)){ System.out.println("Match Found"); matchFound = true; }} if(!matchFound){ BoardState addme = new BoardState(); addme.str = x; addme.cnt = 1; memory.add(addme); } 

} ....

+7
source share
2 answers

The reason it doesn't work is because your BoardState class is an internal, non-static class inside IntelligentTicTacToe . This means that when you access it, you will refer to an instance of the class; instance is not accessible from a static context.

One solution is to declare this class as:

 public static class BoardState { 

You can read more about inner classes here .

+17
source

Do not embed classes like you. There is no need, and all he is going to do is require you to create a BoardState object on top of the IntelligentTicTacToe instance, i.e.

 BoardState addme = new IntelligentTicTacToe(). new BoardState(); 

but this should not be a requirement of your program.

Decision. Place the BoardState class, where it belongs, in your own file. Or make BoardState an enumeration, but then it should contain only constants.

+2
source

All Articles