【问题标题】:non-static variable cannot be referenced from a static context java [duplicate]不能从静态上下文java中引用非静态变量[重复]
【发布时间】:2015-07-06 12:36:18
【问题描述】:

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

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

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

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 并键入诸如 on 或 off 之类的命令,这部分工作正常。我想快速添加一个可选选项,以消除每次输入命令时连接到 http:// * /aruino/digital/13 以加快速度。这是我的开始。我要为它添加一个命令,但在我弄清楚之前我不能这样做

【问题讨论】:

  • Hereherehere 等等。请在发布问题之前搜索您的问题。
  • 首先,试着清理一下你的代码;除了格式问题,您可能还想将一些代码拆分成几个方法
  • 不要留下空的捕获部分。至少打印堆栈跟踪。
  • @Pshemo 声明应该是“不要留下空的捕获部分,至少评论他们的存在/目的”。要求至少一个堆栈跟踪将否定完全可接受的异常使用,例如用于终止转换循环的 NoSuchElement。
  • 只是添加到其他一些答案:问题是static 方法不使用任何实例来运行(我们通过YourClass.staticMethod() 从类运行它)。换句话说在静态方法中没有有效的this(在它的上下文中)所以像this.fast这样的代码(这就是你想要做的,因为fast是非静态字段,这意味着它需要一些实例,如果您不指定任何 this 会自动添加)没有意义,因为没有 this

标签: java variables arduino-yun


【解决方案1】:

只有将static 设为static,才能直接访问class 的成员变量。 Om 创建变量 static 的另一种方法是通过 class_name.variable_name

否则,您必须创建 classobject,然后您才能通过该对象访问该变量。

示例:

  • 要么你改变

    int fast=0;static int fast = 0;

  • 或者你这样做

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 虽然这确实解决了这种情况下的问题...这甚至没有尝试解释该错误的含义或将来如何防止它...请尝试帮助 OP了解他为什么也面临这个问题......
    • 对不起,我会编辑:)
    • “它不能使用任何实例变量”您需要更具体地解释“使用”您的意思是使用而不必指定特定实例(因为静态方法可以使用非静态字段,只要他们有明确的实例,如instance.nonStaticField)。
    • @Pshemo 感谢您提出这一点。会记住的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2013-12-02
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    相关资源
    最近更新 更多