English 中文(简体)
“无法从静态环境中参考的非静态变量”?
原标题:"non-static variable this cannot be referenced from a static context"?

I m a Java newbie and I m trying to deploy a fibonacci trail through recursive function and then calculate the run time. here is the code I have managed to write:

class nanoTime{
    int fib(int n){
        if(n==0) return 0;
        if(n==1) return 1;
        return this.fib(n-1)+this.fib(n-2);
    }
    public static void main(String[] args){
        double beginTime,endTime,runTime;
        int n=10;
        beginTime = System.nanoTime();
        n = this.fib(n);
        endTime = System.nanoTime();
        runTime = endTime-beginTime;
        System.out.println("Run Time:" + runTime);
    }
}

问题在于,我试图将其变为Byte-code I时有以下错误:

nanoTime.java:11: non-static variable this cannot be referenced from a static context

我想知道问题是什么?

最佳回答

变化

n = this.fib(n);

纽约总部

n = fib(n);

并使<代码>fib保持不变。

或者,变化

n = this.fib(n);

纽约总部

n = new nanoTime().fib(n);
问题回答

您需要即时发出<代码>nanotime,以示例,或使fi方法静态。

问题只是信息所说的话。 www.un.org/chinese/ga/president 您也需要使用<代码>fib方法static,然后使用<代码>nanotime.fib(n)。

没有理由在您的法典中使用this

采取步骤:

  1. Rename your class to anything that starts with an upper case letter
  2. Remove all this from your code
  3. Add the static keyword before int fib(int n){
  4. Finally get a good Java book! ;)
# Name the class something else to avoid confusion between System.nanoTime and the name of your class.

class nanoTime1{
    int fib(int n){
        if(n==0) return 0;
        if(n==1) return 1;
        return this.fib(n-1)+this.fib(n-2);
    }

    public static void main(String[] args){
        double beginTime,endTime,runTime;
        int n=10;
        beginTime = System.nanoTime();


        # Instantiate an object of your class before calling any non-static member function

        nanoTime1 s = new nanoTime1();
        n = s.fib(n);
        endTime = System.nanoTime();
        runTime = endTime-beginTime;
        System.out.println("Run Time:" + runTime);
    }
}

careful! 在 Java,主子必须属于一个类别定义,但它只是方案的切入点,绝对不是物体/阶级的一种方法。

更改<代码>

nano obj = new nano();   
n = obj.fib(n);   

<代码>该与该类别的例子有关。 静态方法并不以班级为主,而是与班级本身。 因此,将<代码>fib方法改为static 页: 1





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签