I have created a method that is called readfile. my method should read the Data and run readfile method in the main method. however when i tried to call my method in the main method it shows me cannot make static reference to the non static method readfile (string) from the type DataAnalysis. can someone help me how i can call the method in the main method with out making readfile static method?
4 Answers
readfile doesn't use any instance variables - you should just define it as static:
public static void readfile(String name) { // Your code here... 3 Comments
Stephen C
A solution. But probably not the right one ... in general.
Peter Lawrey
@StephenC I am of the view that any method which can be
static should be, as making an instance method when it doesn't need to be is confusing.Stephen C
Yes ... but my reading of this code is that it is going to turn into something that needs to be an instance method.
either make this function static
try this
public static void readfile(String name) instead Of public void readfile(String name)
and call as like this
DataAnalysis.readfile(String name)
OR
make a object then call the method as like follows
DataAnalysis obj = new DataAnalysis(); obj.readfile("StateCrime.csv");
readFileshould bestaticas it doesn't use any instance fields or methods,