-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のインスタンスを作成し、次のように呼び出す必要があります。

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

次に、メインメソッドでそれを呼び出します。

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

静的メソッド、、、、、およびフィールドを呼び出していurlます。したがって、それらはそれ自体が静的であるか、コンストラクターなどの非静的メソッドから呼び出して、クラスをインスタンス化する必要があります。isdislinemain()main()DjPageDownloader

于 2012-08-26T16:58:34.810 に答える
0

すべてのフィールド宣言をメインメソッド内に移動します。オブジェクト自体を使用しているのではなく、mainメソッドのみを使用しています。

于 2012-08-26T16:57:57.150 に答える
0

静的メソッドは、メソッドの外部からのみ静的メソッドと静的変数にアクセスできます。したがって、mainメソッドから、メソッドの外部からメソッドまたは変数にアクセスする場合は、それらを静的に参照する必要があります。

于 2012-08-26T17:08:33.567 に答える