Question

I did not write this code, All credit goes to the person that is hosting the link. So here is a link to the code and the guy who wrote it is listed on github also... Link -Github SourceCode-

Here is the method preview: (see class below)

//helper method
public static List readFile(String filePath) throws IOException {
    return new TextFileReader(filePath).readFile();
}

Wait a second, I think I may get it now, it's just so someone can invoke the readFile() method without having a dependancy to this object, or am I wrong?

and here is a link to the project on git, if anyone wants a lower level view of the project. I have searched around and I only see helper methods breaking up larger tasks, which was my initial understanding of them. However, I was on github, and found this: (method brought up from bottom of page for easy viewing, but is also in the class code. Plus here is a link to the git if anyone wants to take a better look... Thank you for any replies or edits that looked normal until the static helper method at the bottom of the page

public class TextFileReader implements FileReaderStrategy<String> {

private static final FileType FILE_TYPE = FileType.TEXT_FILE;
private String filePath;

public TextFileReader(String filePath) {
    this.filePath = filePath;
}

@Override
public List<String> readFile() throws IOException {
    List lines = new ArrayList();
    BufferedReader in = null;
    try {
        in = new BufferedReader(
                new FileReader(filePath));
        String line = in.readLine();
        while (line != null) {
            lines.add(line);
            line = in.readLine();
        }
    } catch(IOException ioe){
        throw ioe;
    }finally{
        if(in != null){
            in.close();
        }
    }
    
    return lines;
}

//helper method
public static List readFile(String filePath) throws IOException {
    return new TextFileReader(filePath).readFile();
}

}

Was it helpful?

Solution

You are correct. Although I would say that it's so the method can be called on the Class rather than on an object, or instance, of the class.

For example, static methods can be called like this:

TextFileReader.readFile(<filepath>);

without having to first create an instance of the class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top