0

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?

1
  • readFile should be static as it doesn't use any instance fields or methods, Commented Apr 23, 2016 at 18:36

4 Answers 4

8

You would have to create an instance of the DataAnalysis object.

DataAnalysis da = new DataAnalysis(); da.readfile("StateCrime.csv"); 
Sign up to request clarification or add additional context in comments.

Comments

2

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

A solution. But probably not the right one ... in general.
@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.
Yes ... but my reading of this code is that it is going to turn into something that needs to be an instance method.
1

option1 make the method static

public static void readfile(String name){ 

option2 make an object of the class and call it

DataAnalysis myDataAnal = new DataAnalysis(); myDataAnal.readfile(FILE); 

Comments

0

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"); 

Comments