Hello everybody,
I am happy to find this place recently and now hoping to get some help from you guys.
I tried to write some codes to help my understanding on File class in IO, but encounter some problems which may arise from somewhere else.
Please see the codes below:
import java.io.File;
public class FilePractice
{
File f1=new File("d:\\java2-certificate");
public static void main(String s[]){
boolean temp=f1.exists(); //boolean exists()
System.out.println("existance of f1 is "+temp);
}
}
error: can not make static reference to nonstatic variable f1 in
FilePractice class.
I got the error message as above. After I move "File f1=new File("d:\\java2-certificate");" into main method, everything is fine. I do not quite understand what it really means. Why can't I code like this? ( I am always weak in anythin static in java).
On the other hand, examples in RHE often use import like
import java.io.File. If import java.io.*; can actually import anything you need in io package, why bother to type File. Will it really slow down the process of the program?
Thanks in advance. I appreciate your help.
Ben