Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Non-static variables problem

  Asked By: Bertha    Date: Oct 26    Category: Java    Views: 874
  

I've just remebered another problem I'm having with a bit of the code I have:
The error thrown up by the compiler says that a non-static ariable cannot be
used in a static 'way'.(something along those lines anyway) The variable
referenced to is
directoryFile (although it may have been directory, sorry, haven't the time
to check aain)

BufferedReader directoryFile = null;
try
{
new BufferedReader(new FileReader("config.txt"));
directory = directoryFile.readLine();

}
catch( IOException ioe ){ioe.printStackTrace();}
finally
{
if (directoryFile != null)
try
{
directoryFile.close();
} catch (IOException ioe2)
{
// just ignore it
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Mae Roberts     Answered On: Oct 26

The error  is thrown when you have a static  method and in the body of that method
you try to access a non-static class
variable:

public static void main(String[] args){
myVar = new MyVar();
}

private myVar;


This should cause the same error. It is fixed by changing the last line to:

private static myVar;

 
Didn't find what you were looking for? Find more on Non-static variables problem Or get search suggestion and latest updates.




Tagged: