This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
How did you try to execute it and what error messages did you get?
Joined: Mar 14, 2012
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
this was the error i got, could you please help me find out the problem?
Joined: Mar 17, 2012
You should not be trying to run the code before compiling it first.
Also, I doubt that the code you posted is all the code you tried to run.
Joined: Mar 14, 2012
sorry, but i use net beans.. is there any compile option in net beans?
rahul aditya wrote:it says non-static content this cannot be referenced from static content
And you still can't see what the problem is?
Look at your class definitions. What you have defined are inner classes, which is almost certainly NOT what you want (I can still count the number of inner classes I've defined on my fingers after 11 years of Java).
Your classes PlayerPiece and TilePiece are inner classes, defined inside the GameShape class.
An instance of an inner class needs a reference to an instance of its enclosing class. You need an instance of GameShape to create the instances of PlayerPiece and TilePiece on in lines 27 and 28. Since the main method is static, you don't have an implicit instance of GameShape to create those instances on.
In this example, there is no reason why the classes PlayerPiece and TilePiece should be inner classes. Move them out into their own source files, as top-level classes.
Another way to solve this is to make PlayerPiece and TilePiece nested classes instead of inner classes. A third way would be to change your main method to create an instance of GameShape first, and then to create the PlayerPiece and TilePiece instances on the instance of GameShape. But since there isn't a good reason to make PlayerPiece and TilePiece inner classes, I think making them top-level classes would be the best solution here.