public class Books{
ArrayList<String> booksDB = new ArrayList<String>();
booksDB.ensureCapacity(200000); //Compilation Errors
}
You might be under the impression that this code is executing sequentially (from top to bottom) but that is not the case. What you are doing is creating a private instance field named booksDB that every instance of Books will carry.
Most likely, you want that code to go in the Books constructor (which gets called whenever a new instance of Books is created. Try the following:
public class Books {
private ArrayList<String> booksDB; // this is a field of the Books class
// when we create a new Books instance, we will initialize the booksDB field
public Books() {
booksDB = new ArrayList<String>();
booksDB.ensureCapacity(20000);
}
}
Then use as follows:
public class Main {
public static void main(String[] args) {
Books b = new Books(); // the Books object is constructed, and its private field booksDB is initialized as we specified.
}
}