-6
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
            }
        }

    }

这在编译时显示错误为:-

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)
4

5 回答 5

6

如果您只是从 Main 类中调用它们,请将所有字段设为静态。您不能从静态位置调用实例字段,因为您当前不在实例中。如果您希望它们保持非静态,则必须通过将 DjPageDownloader 放入构造函数中并调用它来创建 DjPageDownloader 的实例,如下所示:

public DjPageDownloader() {
    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
        }
    }
}

然后在你的 main 方法中调用它:

public static void main(String []args){
    new DjPageDownloader();//If you use it more later, store to a variable, but atm you aren't
}
于 2012-08-26T16:56:52.983 回答
3
public static URL url;
public static InputStream is = null;
public static DataInputStream dis;
public static String line;

或者

public class DjPageDownloader {

public static void main(String []args){

    URL url;
    InputStream is = null;
    DataInputStream dis;
    String line;
    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);
        }
    ...
于 2012-08-26T16:59:07.260 回答
1

您正在从静态方法调用urlisdisline字段main()。所以它们要么自己必须是静态的,要么你必须从非静态方法(如构造函数)调用它们,并main()实例化DjPageDownloader类。

于 2012-08-26T16:58:34.810 回答
0

静态方法只能从方法外部访问静态方法和静态变量。因此,从主方法中,如果您想从方法外部访问任何方法或变量,则必须对它们进行静态引用。

于 2012-08-26T17:08:33.567 回答
0

将所有字段声明移到 main 方法中。你没有使用对象本身,只是主要方法。

于 2012-08-26T16:57:57.150 回答