Reputation: 19
I'm trying to make a simple hangman game. The aspect I need help with is declaring the hangman stage, which is an arraylist. After moving this list outside of my main class, I received several syntax error warnings that were not reported before.
I could move it back to the main class but that would mean a bit too much code in one place for my liking. I'd also like to get to the bottom of this.
public static void main(String[] args) {
// omitted game setup code, ArrayList has been imported
}
public class hangMen{
// here is where the error occurs
// all of the lines below show a syntax error
// All the lines have Syntax error on token ";" & ".", @ expected
List<String> stage = new ArrayList<String>();
stage.add("-------");
stage.add("| |");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|___________");}
// The last line has Syntax error, insert ")" to complete method declaration, insert identifier to complete MethodHeaderName, insert "SimpleName" to complete qualified name
I've never seen this come up before, and my teacher is confused too. Any help?
Upvotes: 1
Views: 2802
Reputation: 339442
A class is a template defined at compile-time to be used at runtime for instantiating objects. Like a cookie-cutter is a template for stamping out a series of cookies.
Each time your app makes another object, another HangMen game in this case, you may need some set-up work to be done to get that object ready for work. This work is usually done in a constructor method. The constructor is a special method that is named exactly the same as the class name. The constructor is also special in that it has no return value specified; all other methods must specify a return type or specify void
while a constructor does neither. The Java platform guarantees that the constructor runs until completely done before the object will be put into service.
You can think of the constructor as that moment while cutting out a cookie where you get to decorate the cookie. Only after decoration is complete do you set the cookie on a platter to be eaten, like readying an object to be put to work.
Tip: I find it helpful to label my constructor methods with a comment line. Make it easier to scan when reading code.
This process of creating, preparing, putting-to-work, and eventually stopping a chunk of software at runtime is known as lifecycle, and is one of the key concepts to learn about in programming.
To keep your list of strings around for use after the initial construction, define the variable as a member field on the class.
And, by the way, in modern Java you need not repeat the < String >
on the ArrayList
, as the compiler can figure that out.
Be sure to learn and follow Java naming conventions. A class name should start with an initial uppercase letter. So HangMen
, not hangMen
. In contrast, the variables holding a reference to an object (an instance) start with a lowercase letter.
public class HangMen{
// ----| Members |------------------------
private List< String > stage ;
// ----| Constructor |------------------------
public HangMen() {
this.stage = new ArrayList<>() ;
stage.add("-------");
stage.add("| |");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|___________");}
}
public void report() {
System.out.println( "stage: " ) ;
System.out.println( this.stage.toString() ) ;
}
}
Let's try out this class in a main
method.
HangMen hangMen = new HangMen() ; // Invokes your constructor implicitly. At runtime, the class `HangMen` (note the uppercase `H`) is used to create a new instance (new object). A reference to the object is assigned to the variable named `hangMen` (note the lowercase `h`).
hangMen.report() ; // Invokes the `report` method for this instance (this object).
Notice the option use of this.stage
versus stage
. The this.
makes clear you mean a member variable, not a local variable. The compiler can usually detect on its own when you are referring to a member variable versus a local variable. In prior years I preferred using the this.
to make reading the code quite clear. But now IDEs with their colorization features provide a visual clue as to which variables are members versus local. So you may want to omit the this.
prefix. I showed both here as an example.
Tip: For clarity, do not put a main
method in your HangMen
class. Put it in another class, named something like App
, in its own .java
file. A main
method is basically a necessary evil, just a non-object-oriented hack to get our object-oriented heaven launched from the cold cruel world of hardware, operating-systems, and command-line consoles. Separating, and basically ignoring, the main
method can help in learning Java.
public class App {
public static void main( String[] args ) {
HangMen hangMen = new HangMen() ; // Invokes your constructor implicitly. At runtime, the class `HangMen` (note the uppercase `H`) is used to create a new instance (new object). A reference to the object is assigned to the variable named `hangMen` (note the lowercase `h`).
hangMen.report() ; // Invokes the `report` method for this instance (this object).
}
}
Upvotes: 1
Reputation: 140494
Your code needs to be inside a block. It can't be directly inside the class.
Assuming this is initialization, you can put the add
s inside an initializer block:
List<String> stage = new ArrayList<String>();
{ // Add
stage.add("-------");
stage.add("| |");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|___________");
} // Add
but using initializer blocks is pretty unusual. Putting the initialization inside a constructor is equivalent to using an initializer, but it's a more usual place to do "complex" initialization:
List<String> stage = new ArrayList<String>();
hangMen() {
stage.add("-------");
stage.add("| |");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|___________");
}
Alternatively, you can declare a method:
List<String> stage = buildStage();
List<String> buildStage() {
List<String> stage = new ArrayList<>();
stage.add("-------");
stage.add("| |");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|");
stage.add("|___________");
}
But I would just use Arrays.asList
to declare the list inline:
List<String> stage = new ArrayList<>(Arrays.asList(
"-------",
"| |",
"|",
"|",
"|",
"|",
"|___________"));
Upvotes: 1