Reputation: 1
I am currently coding an RPG using java classes as a side project after completing my OODP course. I have 3 classes which this problem is concerned with. A Job class, a Knight Class and a Skills Class. Knight inherits the properties from the Job class and has a "has a" relationship with the Skills class. The code snippet of Knight Class is shown below. My issue is that the second last line, skills.add(slash); is getting multiple errors. I tested with just an array list of Integers to add Integers to the array list resulting in the same set of errors. Is there something wrong with my code or syntax here?
import java.util.ArrayList;
public class Knight extends Job {
public Knight() {
super("Knight");
}
private ArrayList<Skills> skills = new ArrayList<Skills>();
Skills slash = new Skills(5, 1, "Slash");
skills.add(slash);
}
This is the constructor for the Skills class
public Skills(int dps, int mana, String name) {
this.dps=dps;
this.mana=mana;
this.name=name;
}
These are the errors I have encountered
Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "SimpleName" to complete
QualifiedName
- Syntax error on token ".", @ expected after this token
- Syntax error, insert "Identifier (" to complete
MethodHeaderName
Upvotes: 0
Views: 107
Reputation: 305
You have not put "skills.add(slash)" statement inside a method/constructor/initializer , this is why you are getting error. Simply change the code to -
import java.util.ArrayList;
public class Knight extends Job {
private ArrayList<Skills> skills = new ArrayList<Skills>();
Skills slash = new Skills(5, 1, "Slash");
public Knight() {
super("Knight");
skills.add(slash);
}
}
Upvotes: 1
Reputation: 3755
private ArrayList<Skills> skills = new ArrayList<Skills>();
Skills slash = new Skills(5, 1, "Slash");
skills.add(slash);
Above part or at least skills.add(slash);
should be inside one of the following,
Upvotes: 1
Reputation: 38
change the your knight code as follow
import java.util.ArrayList;
private ArrayList<Skills> skills = new ArrayList<Skills>();
Skills slash = new Skills(5, 1, "Slash");
public class Knight extends Job {
public Knight() {
super("Knight");
skills.add(slash);
}
}
if you want to add more skill on run time, write one method to.
Upvotes: 1
Reputation: 20455
You can't have statement directly inside class. You have to put it in constructor, method or initializer.
In your case just put skills.add(slash);
in constructor.
Upvotes: 1