How do I access an array list inside of a class constructor in java?
collections, java
Solution
You need to move the declaration of `coins` outside the constructor:
private List<String> coins;
public Purse(){
coins = new ArrayList<String>();
}
This would make `coins` a private field of your class, making it accessible from all non-static methods of the `Purse`. Currently, you are creating a local variable `coins` which becomes inaccessible when constructor exits.
Problem
I constructed a `Purse` class with the following: ``` public Purse(){ ArrayList<String> coins = new ArrayList(); } ``` My function to add strings to the object: ``` public void addCoin(String coinName){ coins.add(coinName); } ``` This idea is that each Purse object can hold a list of strings. I cannot, however, access the `ArrayList<String> coins` with my methods. For example, this method I wrote to reverse the order of the strings ``` public void reverse(){ for (int j = coins.size() - 2; j > -1; j--){ coins.add(coins.get(j)); coins.remove(j); } ``` calls the error that the symbol `coins` cannot be found. How do I make it so that my methods can access the array list I made inside of my constructor? Do I need to say something along the lines of `this.coins.add(coinName)` for the `void addCoin` method or `this.coins.add(this.coins.get(j))` for the reverse method?