-1
public class Fisher { 
 public static int LIMIT = 10; // max # of fish that can be caught 
 private int numThingsCaught = 0;


 public int getNumThingsCaught() { return numThingsCaught; } 

} 

This is my second class

public class Fish { 
 public boolean isDesirableTo(Fisher f) { 
  if (Fisher.getNumThingsCaught() < Fisher.LIMIT)
  return true;
  return false; 
 }
}

I am using eclipse and Fisher.getNumThingsCaught() is underlined, when i hover over to see the error the message says: "Cannot make a static reference to the non-static method getNumThingsCaught() from the type Fisher" How can I change my code in my !//Fish//! class to make this code work?

3 Answers3

2

You can either change Fisher.getNumThingsCaught() to f.getNumThingsCaught() because f is a reference to a Fisher object, or you can make that method static (which is not what you want)

So it will look like:

if (f.getNumThingsCaught() < Fisher.LIMIT)
     return true;
return false;

Also another programming tip. When you define a static variable straight away, consider making it final as well. Because I assume you do not want the LIMIT variable to change.

Arbiter
  • 433
  • 2
  • 9
0

In this piece of code: Fisher.getNumThingsCaught() Fisher is the name of the class, not reference to the object. Since getNumThingsCaught method is not static, you need to call it on object instance.

Solution: just change it to f.getNumThingsCaught()

dimoniy
  • 5,820
  • 2
  • 24
  • 22
0

In the Fish class, you need a reference to a Fisher instance. You can do this by:

public class Fish { 
 Fisher fisher = null;
 public boolean isDesirableTo(Fisher f) 
 { 
    fisher = new Fisher();
  if (fisher.getNumThingsCaught() < Fisher.LIMIT)
  {
     return true;
  }
     return false; 
  }
 }

if (f.getNumThingsCaught() < Fisher.LIMIT)
     return true;
return false;

Add the final modifier to 'LIMIT' to ensure it cannot be overwritten.

leroneb
  • 75
  • 7