简体   繁体   中英

Issue with passing in a File as a parameter

I have a class ArrayListDVDCollection that is extended from my application class file DVDApplication . The ArrayListDVDCollection class implements an interface with just the method declaration for loadData . Inside of my application file where I will run the actual program, I wanted to make a File object that would be able to be passed into the loadData method. However, it keeps giving me errors and stating that the method itself needs to be static. How can I successfully pass in a file into my loadData method in the application file?

public class ArrayListDVDCollection implements DVDCollectionInterface
{  
    public ArrayList<DVD> loadData(File dvdData){
        try{
            BufferedReader kbd = new BufferedReader(new FileReader(dvdData)); // Open the DVDCollection file.
            String line;
            while ((line = kbd.readLine()) != null) // Read the contents.
            {
                String dvdTitle = line;
                String dvdCategory = kbd.readLine();
                String dvdRunningTime = kbd.readLine();
                int dvdYear = Integer.parseInt(kbd.readLine());
                double dvdPrice = Double.parseDouble(kbd.readLine());

                DVDArrayList.add(new DVD (dvdTitle, dvdCategory, dvdRunningTime, dvdYear, dvdPrice));
            }
            kbd.close(); // Close file after reading
        }catch (Exception e){
            System.out.println("Error reading file.");
        }
        return DVDArrayList;
    }
}

Application file

public class DVDApplication extends ArrayListDVDCollection{
    public static void main(String[] args){
        ArrayList<DVD> DVDArrayList = new ArrayList<DVD>();
        File dvdData = new File("DVDCollection.txt");
        DVDArrayList = loadData(dvdData);
    }
} 
public class DVDApplication extends ArrayListDVDCollection{
public static void main(String[] args){
    ArrayList<DVD> DVDArrayList = new ArrayList<DVD>();
    File dvdData = new File("DVDCollection.txt");
    DVDArrayList = new ArrayListDVDCollection ().loadData(dvdData);
}

}

because loadData is non-static method and you need to call it via reference of ArrayListDVDCollection .

The quick and dirty solution is:

public class DVDApplication extends ArrayListDVDCollection{
    public static void main (String[] args){
        ArrayList<DVD> DVDArrayList = new ArrayList<DVD>();
        File dvdData = new File ("DVDCollection.txt");
        DVDApplication dvdapp = new DVDApplication ();
        DVDArrayList = dvdapp.loadData (dvdData);
    }
} 

Since the method loadData isn't static, you need an Instance, to call the method on it.

Since the parent class, you're using, doesn't access member variables, an alternative approach would be, to make the method static there:

public class ArrayListDVDCollection implements DVDCollectionInterface
{  
    public static ArrayList<DVD> loadData(File dvdData) {

Then you could have left your main method unchanged.

There is strong connection between having member variables and methods, depending on their state, and the methods being static or not, and calling them in a static way and static context.

In this case, only the keywords didn't reflect the independence from any mutable state. Maybe this could have been solved otherwise by Java, like automatic dependency checking without static keywords - maybe it would be to costly. Surely it is a way of documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM