import java.net.*;
import java.io.*;
public class DjPageDownloader {
public URL url;
public InputStream is = null;
public DataInputStream dis;
public String line;
public static void main(String []args){
try {
url = new URL("http://stackoverflow.com/");
is = url.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((line = dis.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
this on compilation shows error as:-
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static field url
Cannot make a static reference to the non-static field is
Cannot make a static reference to the non-static field url
Cannot make a static reference to the non-static field dis
Cannot make a static reference to the non-static field is
Cannot make a static reference to the non-static field line
Cannot make a static reference to the non-static field dis
Cannot make a static reference to the non-static field line
Cannot make a static reference to the non-static field is
at djPageDownloader.DjPageDownloader.main(DjPageDownloader.java:16)
Make all of your fields static if you are just calling them from the Main class. You can’t call an instance field from a static position, as you aren’t currently in an instance. If you wish to keep them non-static, you have to make an instance of DjPageDownloader by putting it in a constructor, and calling that, like so:
Then call that in your main method: