non-static variable this cannot be referenced from a static context

元气小坏坏 提交于 2019-11-29 04:14:52
Alex Yarmula

The reason it doesn't work is because your class BoardState is an inner, non-static, class inside of IntelligentTicTacToe. This means that when referring to it, you'll be referring to an instance of the class; the instance isn't available from a static context.

One solution is to declare that class as:

public static class BoardState {

You can read more on inner classes here.

Don't nest classes like you're doing. There's no need, and all it's going to do is to require that you create a BoardState object on top of an IntelligentTicTacToe instance, i.e.,

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

but this should not be a requirement of your program.

Solution: Put the BoardState class where it belongs, in its own file. Or make BoardState an enum, but then it should only hold constants.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!