“非静态变量,不能从静态上下文引用”? [英] "non-static variable this cannot be referenced from a static context"?
本文介绍了“非静态变量,不能从静态上下文引用”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一名Java新手,我正试图通过递归函数部署斐波那契线,然后计算运行时间。
这里是我设法编写的代码:
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);
}
}
问题是当我试图把它转过来到字节码我得到以下错误:
The problem is when I'm trying to turn it into Byte-code I get the following error:
nanoTime.java:11: non-static variable this cannot be referenced from a static context
我想知道是什么问题?!
I'm wondering what is the problem?!
推荐答案
更改
n = this.fib(n);
到
n = fib(n);
并使方法 fib
static。
and make the method fib
static.
或者,更改
n = this.fib(n);
到
n = new nanoTime().fib(n);
这篇关于“非静态变量,不能从静态上下文引用”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文