0

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
4
  • You aren't in a method, constructor or initialization block. You are currently defining another class field named slash with package private access. I suspect you wanted to put skills.add(new Skills(5,1,"Slash")); in your constructor. Commented Jan 7, 2019 at 4:08
  • Put these lines private ArrayList<Skills> skills= new ArrayList<Skills>(); Skills slash= new Skills(5,1,"Slash"); skills.add(slash); in a method/initialization block/constructor. Commented Jan 7, 2019 at 4:09
  • 1
    omg I am not in a method and this is not a main class..... Thanks, I must be tired haha Commented Jan 7, 2019 at 4:09
  • Please read error messages carefully. It tells you have a syntax error with the specified identifiers. You can also read about how Java compiler works. Commented Jan 7, 2019 at 7:24

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1
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,

  • Inside a method
  • Inside the main method
  • Inside a constructor
  • Inside an instance initializer block or in a static initializer block

2 Comments

Not all three, only skills.add(slash); should be in the method, constructor or static block. Other two declarations can be inside class.
@Santosh Edited it.
1

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);
         } 

     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.