HunderingThooves
HunderingThooves

Reputation: 1002

Proper initialization of an arraylist in an object?

Assuming that I'm running with an object like this like this:

static class fileHandler {
    File fileToHandle;
    ArrayList fileDetails;

    fileHandler(File fileIn) {
        fileToHandle = fileIn;
    }


    public void fileHandling() {

        try {
            Scanner reader = new Scanner(fileToHandle);
            reader.useDelimiter(",");
            while(reader.hasNext()) {
                String s = reader.next();
                fileDetails.add(s);
            }
        } catch (FileNotFoundException e) { System.err.println("File Not Found!"); }
    }
}

How could I make "fileDetails" able to work inside my method?

Upvotes: 0

Views: 827

Answers (1)

juergen d
juergen d

Reputation: 204894

Initialize it in your Constructor

fileHandler(File fileIn) {
    fileToHandle = fileIn;
    fileDetails = new ArrayList();
}

or right on definition:

ArrayList fileDetails = new ArrayList();

By the way you should use a generic ArrayList:

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

and class names should start with a Uppercase Letter

class FileHandler { 

Upvotes: 2

Related Questions