DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  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?

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links