不能从静态上下文中引用非静态变量 java

non-static variable cannot be referenced from a static context java

我正在编写一个程序以使用 java 连接到控制台像素示例草图。我还是很新,我收到了这个错误:

non-static variable fast cannot be referenced from a static context

我不知道错误是什么意思,但我的代码是:

package javaapplication5;

import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
 *
 * @author preferreduser
 */
public class JavaApplication5 {
    int fast = 0;
    public static void main(String[] args) throws IOException {
        Scanner x = new Scanner(System.in);
        System.out.print("Yun ip: ");
        String IP = x.nextLine();
        System.out.println("Loding...");
        try {
            // Create a URL for the desired page
            URL url = new URL("http://"+ IP +"/arduino/digital/13/1");       

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        try {
            // Create a URL for the desired page
            URL url = new URL("http://"+ IP +"/arduino/digital/13/0");       

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        System.out.println("Connected to YUN on "+ IP);
        OUTER:
            while (true) {
                Scanner y = new Scanner(System.in);
                System.out.print("> ");
                String str = y.nextLine();
                switch (str) {
                case "on":
                    try {
                        // Create a URL for the desired page
                        URL url = new URL("http://"+ IP +"/arduino/digital/13/1");

                        // Read all the text returned by the server
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        in.close();
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    }               break;
                case "off":
                    try {
                        // Create a URL for the desired page
                        URL url = new URL("http://"+ IP +"/arduino/digital/13/0");

                        // Read all the text returned by the server
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        in.close();
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    }               break;
                case "help":
                    System.out.println("");
                    System.out.println("on   exit");
                    System.out.println("off  help");
                    System.out.println("");
                    break;
                case "exit":
                    try {
                        // Create a URL for the desired page
                        URL url = new URL("http://"+ IP +"/arduino/digital/13/0");

                        // Read all the text returned by the server
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        in.close();
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    }               break OUTER;
                }
                if ( fast == 1 ){
                    URL oracle = new URL("http://"+ IP +"/arduino/digital/13");
                    try (BufferedReader in = new BufferedReader(
                            new InputStreamReader(oracle.openStream()))) {
                        String inputLine;
                        while ((inputLine = in.readLine()) != null)
                            System.out.println(inputLine);
                    }
                } else {System.out.println("Success");}
            }
    }
}

我想连接到 arduino yun 并输入打开或关闭等命令,这部分工作正常。我想快速添加一个可选选项,以消除每次键入命令时连接到 http:// * /aruino/digital/13 的情况,从而加快速度。这是我的开始。我要为它添加一个命令,但在我弄清楚之前我不能

一个class的成员变量只需要使它成为static就可以直接访问。 Om 使变量 static 另一种访问它的方法是 class_name.variable_name.

否则你必须创建 classobject 然后你可以通过那个对象访问那个变量。

示例:

  • 要么你改

    int fast=0;static int fast = 0;

  • 或者你这样做

    JavaApplication5 j5 = new JavaApplication5(); 现在通过 j5.fast 访问变量并进行进一步计算。

int fast = 0; 更改为 static int fast = 0;

您正在 main 方法中使用变量 fast,这是一个静态方法。任何静态方法中使用的所有变量都应该是静态的。原因是静态方法是common for a class,它不依赖于class的实例。因此它不能在其中使用任何实例变量(除非您指定要使用哪个特定实例),因为该方法不知道要使用哪个实例变量。