Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java error: Non Static reference

Options
  • 31-01-2008 4:47pm
    #1
    Registered Users Posts: 7,986 ✭✭✭


    So I was reading my Deitel&Deitel Java book and decided to try out one of the examples/ Its for printing out the values of Fibonacci numbers.
    public class FibonacciCalculator
    {
       
      public long fibonacci( long number )
      {
        if ( ( number == 0 ) || ( number == 1 ) )
          return number;
        else
          return fibonacci( number - 1 ) + fibonacci( number - 2);
      }
    
      public void displayFibonacci( )
      {
        for ( int counter =0; counter <= 10; counter++ )
          System.out.printf( "Fibonacci of %d is: %d\n" , counter, 
          fibonacci( counter ) );
      }
     
    }
    
    public class FibonacciTest
    {
      
      public static void main( String[] Args )
      {
    
        FibonacciCalculator fibonacciCalculator = new FibonacciCalculator();
        FibonacciCalculator.displayFibonacci( );
      }
    }
    

    The problem is that the line "FibonacciCalculator.displayFibonacci( );" in the main method won't work because the method "display.Fibonacci" is not static. The error message is returned is:
    non-static method displayFibonacci() cannot be referenced from a static context
    

    Acccording to the book the output should be as follows

    Fibonacci of 0 is: 0
    Fibonacci of 1 is: 1
    Fibonacci of 2 is: 1
    Fibonacci of 3 is: 2
    Fibonacci of 4 is: 3
    .....
    Fibonacci of 10 is: 55

    Is there any way to change the method displayFibonacci to be a variable?


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    It should be
    fibonacciCalculator.displayFibonacci( );
    

    not
    FibonacciCalculator.displayFibonacci( );
    

    Java is case sensitive and that's not the clearest example if you're typing it out yourself.
    public class FibonacciTest
    {
      
      public static void main( String[] Args )
      {
    
        FibonacciCalculator fibbCalc = new FibonacciCalculator();
        fibbCalc.displayFibonacci( );
      }
    }
    


  • Registered Users Posts: 7,986 ✭✭✭youcancallmeal


    Sorry I'm an idiot should of seen that after trying to figure it out for half an hour :(
    It wasnt my own coding its taken from the book so you could say their examples are not the clearest?
    Anyway cheers!


  • Moderators, Science, Health & Environment Moderators Posts: 10,076 Mod ✭✭✭✭marco_polo


    Sorry I'm an idiot should of seen that after trying to figure it out for half an hour :(
    It wasnt my own coding its taken from the book so you could say their examples are not the clearest?
    Anyway cheers!

    Its just an ok book really, not terrible by any means, and it was my first java book so I have a soft spot for it. But in general I found that many of the examples are often way too complicated when they are just trying to illustrate a relatively a simple concept.


  • Closed Accounts Posts: 6,151 ✭✭✭Thomas_S_Hunterson


    Just don't try it for any anything above about the 45th term, as it's horrendously inefficient and will take ages to complete.

    I presume it's as a demonstration of recursion yes?


  • Registered Users Posts: 7,986 ✭✭✭youcancallmeal


    Sean_K wrote: »
    Just don't try it for any anything above about the 45th term, as it's horrendously inefficient and will take ages to complete.

    I presume it's as a demonstration of recursion yes?

    Yep. I'm writing a 2500 word essay on recursion. Its possibly the worst essay I've ever had to write and I had to handwrite a 1000 word essay on traffic cones(long story) :(


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 10,076 Mod ✭✭✭✭marco_polo


    Yep. I'm writing a 2500 word essay on recursion. Its possibly the worst essay I've ever had to write and I had to handwrite a 1000 word essay on traffic cones(long story) :(

    Wierd topic however fear not. As I will demonstrate, 2500 words is no problem:

    ""method A() calls method A() which calls method A() which calls method A() ................... And they all lived happily ever after"
    Best of luck ;)


Advertisement