Marcus Taylor
Marcus Taylor

Reputation: 43

How to add an ArrayList at the class level?

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.

Upvotes: 3

Views: 2034

Answers (5)

0xCursor
0xCursor

Reputation: 2268

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

Upvotes: 5

Robby Cornelissen
Robby Cornelissen

Reputation: 97130

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

Upvotes: 3

Peteef
Peteef

Reputation: 405

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

Upvotes: 2

karakale
karakale

Reputation: 146

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) {

    }
}

Upvotes: 0

Gratien Asimbahwe
Gratien Asimbahwe

Reputation: 1614

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

Upvotes: 0

Related Questions