3

I'm attempting to make a DnD character generator, and for part of that I need to have a list of stats that are editable by other methods, so I am attempting to add it at the class level. My current code is:

public class CharacterCreator extends Application
{
    ArrayList<String> stats = new ArrayList<String>();

        stats.add("STR");
        stats.add("DEX");
        stats.add("CON");
        stats.add("INT");
        stats.add("WIS");
        stats.add("CHA");

public void start(Stage primaryStage)
{

But when I try to run it, I get a "identifier expected" error in on every 'add' line.

5 Answers5

5

You can initialize the ArrayList like this:

ArrayList<String> stats = new ArrayList<>(Arrays.asList("STR", "DEX"));

as is shown in this answer. Or, just put the add calls inside a method or constructor like this:

ArrayList<String> stats = new ArrayList<>();

// Adding to ArrayList inside a constructor
public CharacterCreator()
{
    stats.add("STR");
    stats.add("DEX");
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
3

You cannot have bare statements like that in the class body. You need to populate the array in a method, constructor or initialization block:

public class CharacterCreator extends Application{
    ArrayList<String> stats;

    public CharacterCreator() {
         stats = new ArrayList<String>();
         stats.add("STR");
         stats.add("DEX");
         stats.add("CON");
         stats.add("INT");
         stats.add("WIS");
         stats.add("CHA");
    }
}

If you want the stats to be defined on the class level instead of the object level, you'll need to add a static initialization block:

public class CharacterCreator extends Application{
    static ArrayList<String> stats;

    static {
         stats = new ArrayList<String>();
         stats.add("STR");
         stats.add("DEX");
         stats.add("CON");
         stats.add("INT");
         stats.add("WIS");
         stats.add("CHA");
    }
}
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
2

What about initializing block?

public class CharacterCreator extends Application {
    public ArrayList<String> stats = new ArrayList<String>();
    {
       stats.add("STR");
       stats.add("DEX");
       stats.add("CON");
       stats.add("INT");
       stats.add("WIS");
       stats.add("CHA");
    }
}
Peteef
  • 377
  • 1
  • 2
  • 10
0

as @shmosel just said you can't run statements outside a method. you can create an initializer method and call it in the constructor or move instructions into the constructor. add method is considered as unknown and so can't be resolved

Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
0

You can not call add() method while declaration of class. Use static block or call method in constructor.

public class CharacterCreator extends Application {

    ArrayList<String> stats = new ArrayList<String>();

    public CharacterCreator() {
        stats.add("STR");
        stats.add("DEX");
        stats.add("CON");
        stats.add("INT");
        stats.add("WIS");
        stats.add("CHA");
    }

    public void start(Stage primaryStage) {

    }
}
karakale
  • 126
  • 11