0

I tried writing a small program, but it will not compile. Eclipse shows the following errors on the last line:

  • Syntax error on token(s), misplaced construct(s)
  • Syntax error on token "200000", delete this token

    import java.util.ArrayList;
    public class Books{
      ArrayList<String> booksDB = new ArrayList<String>();
      booksDB.ensureCapacity(200000);        //Compilation Errors
    }
    
1
  • That line needs to go in a method. Commented May 31, 2013 at 23:25

4 Answers 4

1
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.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Except for declarations and imports, all code in Java must reside inside a method (or, as discussed below, inside initializers, which are essentially methods too):

import java.util.ArrayList;
public class Books {
    ArrayList<String> booksDB = new ArrayList<String>();
    public void doSomething() {
        booksDB.ensureCapacity(200000);        //Compilation Errors
    }
}

Since you seem to be initializing the contents of the object, you might want to put that line in a constructor, which is a method that is automatically run on newly-created objects:

import java.util.ArrayList;
public class Books {
    ArrayList<String> booksDB = new ArrayList<String>();
    public Books() {
        booksDB.ensureCapacity(200000);        //Compilation Errors
    }
}

8 Comments

What about static {...}?
@arshajii: A static constructor is also a method.
I'm referring to static initialization blocks.
There is no such thing as a "static constructor", I think you mean static initializer (docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.7)
But I don't think it should be considered a 'method' in the normal sense. You cannot, for instance, 'call' a static initialization block like you can a method.
|
1

You can use constructor of ArrayList that accepts initial capacity like below

import java.util.ArrayList;
public class Books{
   ArrayList<String> booksDB = new ArrayList<String>(200000);
}

Comments

0

Access variables ( booksDB ) only from methods. That's all you have to do.

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.