5

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?!

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
2hamed
  • 8,719
  • 13
  • 69
  • 112
  • You should use CamelCase for class names... call it `NanoTime` next time. – mellamokb Oct 03 '11 at 17:00
  • +1 @JB Nizet - very often googling for your exception and the message you get gives you good result and solution for your problem. This is true for beginners' questions (like this one) or more obscure problems – Guillaume Oct 03 '11 at 17:07
  • Similar to http://stackoverflow.com/questions/926822/java-non-static-variable-cannot-be-referenced-from-a-static-context-error – Kelly S. French Oct 03 '11 at 18:01

7 Answers7

12

Change

n = this.fib(n);

to

n = fib(n);

and make the method fib static.

Alternatively, change

n = this.fib(n);

to

n = new nanoTime().fib(n);
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Mouscellaneous
  • 2,584
  • 3
  • 27
  • 37
3

You need to instantiate a nanoTime to call an instance method, or make the fib method static as well.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    unfortunately I have no clue of what you're talking about! – 2hamed Oct 03 '11 at 17:00
  • @James See Mouscellaneous' answer; in general for homework stuff I only point, not spell :) – Dave Newton Oct 03 '11 at 17:04
  • @DaveNewton Even if you make the fib method static, it will throw a compile time error saying "nanoTime.this cannot be referenced from a static context" . Reason: They keyword this refers to the instance of the class. In a static context, you have no instance, therefore you can't refer it. Source: https://stackoverflow.com/questions/16315488/this-cannot-use-this-in-static-context – Ankit Bhatia Aug 15 '17 at 12:42
  • @Joey Obviously the code would need to be changed to remove the `this` context if the method is made static... :/ I'm aware of the differences. – Dave Newton Aug 15 '17 at 12:45
3

The problem is just what the message says. Your main method is static, which means it is not linked to an instance of the nanoTime class, so this doesn't mean anything. You need to make your fib method static as well, and then use nanoTime.fib(n).

mellamokb
  • 56,094
  • 12
  • 110
  • 136
3

There is no reason to use this in your code.

Steps to take:

  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! ;)
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
2
# 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);
    }
}

Qiu
  • 5,651
  • 10
  • 49
  • 56
gibraltar
  • 1,678
  • 4
  • 20
  • 33
1

Be careful ! In Java the main has to be in a class definition, but it's only the entry point of the program and absolutely not a method of the object/class.

Santana6.35
  • 198
  • 1
  • 3
  • 15
  • What does that mean? `main` most certainly is a static method on the class and can be called like any other method. The JVM can use it as an entry point, but it's still just a method. – Dave Newton Aug 15 '17 at 12:47
1

Change this.fib(n) to :

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

this is associated with the instance of the class. A static method does not run with a class instance but with the class itself. So either change the fib method to static or replace the line where you call fib to the above two lines.

Qiu
  • 5,651
  • 10
  • 49
  • 56
I J
  • 805
  • 3
  • 9
  • 19