'Java "VariableDeclaratorId expected after this token"
I'm working on a little project for fun, that's essentially a little combat emulator. I'm trying to use a class similar to struct in C++, as in using it to create an object (in this case, a character or "entity", as the class is called). I'm getting the error in the title when attempting to call any integer in said class from the main function.
class entity{
public int health;
public int accuracy;
public int power;
public int defense;
}
And
public class Tutorial {
static Random rnd = new Random();
entity player;
player.health = 100; // Issue on the health part
player.accuracy = 19; // Issue on the accuracy part
player.power = 15; // Issue on the power part
player.defense = 18; // I think you get it by now...
I've been looking around for a while to find some explanation, but there's none that I could find that explain the nature of the error as well as possible fixes for my situation. If I could get those, it would be wonderful.
Solution 1:[1]
The compiler is expecting a variable declaration on the line
player.health = 100;
but is finding an assignment instead. The statements
Entity player = new Entity();
player.health = 100;
player.accuracy = 19;
player.power = 15;
player.defense = 18;
should be in a code block such as a method or constructor rather than the class block
Solution 2:[2]
Procedural code cannot be written directly in a class definition. The code is causing syntax errors because it's not legal to do so.
Instead, put the code in an appropriate method or initialization block. (I do not think an initialization block is appropriate here, so I am trivially showing a "factory method".)
As such, consider an approach like
// This is a member variable declaration, which is why it's OK
// to have a (static) method call provide the value.
// Alternatively, player could also be initialized in the constructor.
Entity player = makeMeAPlayer();
static Entity makeMeAPlayer() {
// Create and return entity; the code is inside a method
Entity player = new Entity();
player.health = 100;
// etc.
return player;
}
(I've also cleaned up the type to match Java Naming Conventions - follow suite!)
Solution 3:[3]
Here is another way to suffer from highly accurate yet not terrible intuitive messaging for VariableDeclaratorId.
@PostMapping("/showCompany")
public String processForm(@ModelAttribute("company") CompaniesDAO company, company_name, company_function) {
Will produce:
Syntax error, insert "... VariableDeclaratorId" to complete FormalParameter
This is telling you that is need the variable type like String or int...
@PostMapping("/showCompany")
public String processForm(@ModelAttribute("company") CompaniesDAO company, String company_name, String company_function) {
It turns out VariableDeclaratorId is a class
public class VariableDeclaratorId
extends Expression
implements prettyprint.PrettyPrintable
If you read the class description using it with the error message to determine that you forgot to define the variable type, I am impressed; I simply made an educated guess.
Solution 4:[4]
It is because you forgot to add the main method,
public class Tutorial {
public static void main(String[] args) {
static Random rnd = new Random();
entity player;
player.health = 100; // Issue on the health part
player.accuracy = 19; // Issue on the accuracy part
player.power = 15; // Issue on the power part
player.defense = 18; // I think you get it by now...
}
}
and
class entity{
public int health;
public int accuracy;
public int power;
public int defense;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Community |
Solution 3 | Richard Bradley Smith |
Solution 4 | Anil Nivargi |