Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does it think this is a static context? 1

Status
Not open for further replies.

KirbyWallace

Programmer
Dec 22, 2008
65
US
For some reason, the line "FileToConsole.fileToConsole(cInputFile);" is
generating that pesky "non-static method fileToConsole() cannot be
referenced from a static context" error.

But I cannot figure out what makes it think .FileTest() is static.
Does it consider all constructors to be static by default?

I'm a complete beginner to java, but I have some background in other,
non-oo languages.

Note: PACKAGE and IMPORTs omitted for brevity **


//Main.java
//--------------------
Code:
public class Main {
    public static void main(String[] args) {
        FileTest ft = new FileTest(args);
    }
}



// FileTest.java
Code:
public class FileTest {
    public FileTest(String[] args) {
        String cInputFile = "test.txt";
        FileToConsole.fileToConsole(cInputFile);
    }
}


//FileToConsole.java
//----------------------------
Code:
public class FileToConsole {

    public void fileToConsole(String cFile) {

        try {

            BufferedReader input =  new BufferedReader(
									new FileReader(
									new File(cFile)));

            String line = null;

            while (( line = input.readLine()) != null) {
                 System.out.println(line);

            }
        }

        catch (IOException ex) {
            System.out.println("File Not Found");
        }
    }
}

Any input appreciated!

 
Try to call fileToConsole(cInputFile) like this:
Code:
public class FileTest {
    public FileTest(String[] args) {
        String cInputFile = "test.txt";
        FileToConsole fileToConsoleObj = new FileToConsole();
        fileToConsoleObj.fileToConsole(cInputFile);
    }
}
fileToConsoleObj is an object reference.
The following link is about object reference
 
You need to have an object reference to call the method of that object except the method of of that object is defined as static.

The following is the second solution.
Code:
public class FileToConsole {

    public static void fileToConsole(String cFile) {

        try {

            BufferedReader input =  new BufferedReader(
                                    new FileReader(
                                    new File(cFile)));

            String line = null;

            while (( line = input.readLine()) != null) {
                 System.out.println(line);

            }
        }

        catch (IOException ex) {
            System.out.println("File Not Found");
        }
    }
}
 
KirbyWallace said:
But I cannot figure out what makes it think .FileTest() is static.
Maybe the fact that you're calling the method as static
Code:
FileToConsole.fileToConsole(cInputFile);

Cheers,
Dian
 
fileToConsole with initial lowercase can't be a constructor, and is therefore, and because of the returntype void, a method.

You need to have an object of that type, to call its methods, and you create one with the keyword new.

Code:
   public FileTest(String[] args) {
        String cInputFile = "test.txt";
        FileToConsole.fileToConsole(cInputFile);
    }
why is args used here?

Code:
        catch (IOException ex) {
            System.out.println("File Not Found");
        }
a) You should use System.err. for errormessages, and b) you shouldn't tell things which you don't know - "file not found". There are other possible reasons - "permission denied" for instance.

ex.getMessage () will tell the cause.
ex.printStackTrace () is more useful for the programmer.

Lerning java from a book is more appropriate - we may help occassionally, but not in every small aspect, I think.

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top