Reputation: 29
class Room {
private String description;
private HashMap<String,Room> dir=new HashMap<String,Room>();
Room(String de){
description=de;
}
public String toString() {
return description;
}
public void add(String s,Room r) {
dir.put(s, r);
}
}
class Game {
Room lobby; //Syntax error on token";",,expected
lobby=new Room("pub");
}
There are two classes.And the Game class have a problem that says Syntax error on token";",,expected.i am confused.
Upvotes: 2
Views: 3708
Reputation: 58892
Following Java's Initializing Fields doc, there are three options:
1- Initialize on declaration:
Room lobby=new Room("pub");
2- Static Initializer blocks:
static {
lobby=new Room("pub");
}
3- Initializer blocks:
{
lobby=new Room("pub");
}
Upvotes: 1
Reputation: 658
Java allows initialization only during
JVM allows allocation memory only during the above steps. In the example you have provided, lobby=new Room("pub");
since its in Class level JVM doesn't know how to allocate memory resulting an error.
Upvotes: 1
Reputation: 1402
Instead of:
Room lobby; //Syntax error on token";",,expected
lobby=new Room("pub");
Use inline declaration and instantiation:
Room lobby = new Room("pub");
You can only have a statement inside a block of code {} (methods, static/instance blocks, other constructs that use a block) in a class.
Upvotes: -1