non-static method openFile() cannot be referenced from a static content


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: non-static method openFile() cannot be referenced from a static content

  1. #1
    Join Date
    Nov 2004
    Posts
    11

    non-static method openFile() cannot be referenced from a static content

    How am I allowed to reference open file?

    import java.io.*;

    public class fileManager{
    public String openFile(){
    try{
    String line;
    StringBuffer document = new StringBuffer();
    File inFile = new File("transactionlist.txt");
    FileReader fileReader = new FileReader(inFile);
    BufferedReader bufReader = new BufferedReader(fileReader);

    while (true) {
    line = bufReader.readLine();

    if (line == null) break;

    document.append(line);
    }
    }
    catch(IOException e) {
    System.out.println("File not found");
    }
    return null;
    }
    public String returnOpen(){
    return fileManager.openFile();}
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    You're attempting to access a non-static method from the static context. By this I mean you're accessing the method via the class, rather than via an instance of the class. The way around this is to either create an instance of the class and use this to access the method, or to make the method static.

    Before you do this though, why do you have a method that does nothing more than return the result of a different public method. The method returnOpen() is totally redundant because it doesn't do anything that the method openFile() doesn't do. You might as well get rid of it.

  3. #3
    Join Date
    Nov 2004
    Posts
    11
    But how do I change my code to fix the problem?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    Well either make openFile() static, or make an instance of FileManager and use that to call openFile().

    Either way, there's no need to have the method returnOpen() because all it does is call openFile(). Why not just call openFile() since it's a public method?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center