0

I know this was asked so many times but I can't get the grasp of it. I have this method:

public static void toFile() throws FileNotFoundException{
    PrintWriter oFile = new PrintWriter("Output.csv");
    oFile.println("Tipas,Vadovas,Pavadinimas,Produktas(MB),Darbuotojai(UAB)");
    Company Com = companyList.get(companyList.size()-1);
    Company.csv(Com, oFile);
    oFile.close();
}

On the line Company.csv(Com, oFile); I get error non-static method cannot be referenced from a static context

csv method looks like this

public void csv(Company Com,PrintWriter oFile){
       oFile.print("Kita");
       oFile.println(b+","+n+","+","+"-"+","+"-"); 
   }

As I understood I should have instance variables when passing but I think I can't do this (am I right?)

*NOTE: Company class have two child classes with csv method. Don't know if this helps.

user3102621
  • 425
  • 2
  • 4
  • 9
  • The other questions answer this well enough. In short, you cannot access instance methods without an instance! – awksp May 29 '14 at 22:47
  • user3580294 I do understand this but how to make them instance? – user3102621 May 29 '14 at 22:49
  • Same way you make an instance for any other object -- ` = new (....);` – awksp May 29 '14 at 22:50
  • Currently your `csv` method is an *instance* method. The problem is that by prefixing the call to it with the class name (`Company` in your case), you're specifying that you want to call a `static` method in the `Company` class called `csv`. I posted an explanation of `static` vs `non-static` [here](http://stackoverflow.com/questions/23860661/cannot-make-a-static-reference-to-the-non-static-method/23860891#23860891) that you might find useful if you're struggling with the concept. – JonK May 29 '14 at 22:58

1 Answers1

0

I think what you actually want is to change this:

Company Com = companyList.get(companyList.size()-1);
Company.csv(Com, oFile);

to this:

Company Com = companyList.get(companyList.size()-1);
Com.csv(oFile);

and then change the csv method to this:

public void csv(PrintWriter oFile){

I think this is correct because the csv method is printing out instance variables that are contained within the Com instance -- am I right?

Jake Toronto
  • 3,524
  • 2
  • 23
  • 27
  • The oFile is the main problem here. I did as you wrote but the error is the same – user3102621 May 29 '14 at 22:59
  • If you get rid of `Company.csv(Com, oFile)` and replace it with `Com.csv(oFile)`, then you should no longer see that error. – Jake Toronto May 29 '14 at 23:02
  • Oh missed that bit. Thanks it works, but can you give a little bit more information on why this works? I want to understand it clearly. – user3102621 May 29 '14 at 23:10
  • In your example, you have a class called `Company` and one _instance_ of that class. You called the instance `Com`. In reality, you could have many more instances, such as `com1`, `com2`, etc, and each instance would have its own copy of the variables (it appears you have `n` and `b` as variables. Originally, you were trying to invoke the `toFile()` method on the `Company` class, but the `Company` class doesn't have access to the actual values of the `Com` instance: instead, the `Company` class simply defines what the variable names and types are. – Jake Toronto Jun 02 '14 at 16:46