I am new to java and having an issue calling a non static method
This is my main
public static void main(String[] args) {
Fish f1 = new Fish("Nemo");
Fish f2 = new Fish("Dory");
f2.setNumber(2);
Fish m = new Fish("Bruce");
m.setNumber(3);
Fish.printAllFish();
}
This is my fish class
import java.util.ArrayList;
import java.util.List;
public class Fish {
protected String name;
protected int number;
protected List<Fish> fishList = new ArrayList<Fish>();
public Fish(String in){
name = in;
number = 1;
fishList.add(this);
}
public void printFish(){
System.out.println("the fish called" + name + " is number " + number );
}
public void setNumber(int Number){
this.number = number;
}
public int getNumber(){
return number;
}
public String getName(){
return name;
}
public int getFishNumOf(){
return fishList.size();
}
public void printAllFish(){
int size = this.getFishNumOf();
System.out.println("There are " + size + " fish:");
for (int i = 0; i < size; i++){
String a = getName();
int b = getNumber();
System.out.println("The Fish " + a + " is number " + b );
}
}
}
I get the nonstatic error when trying to call printAllFish, I know I am probably making some rookie mistakes but I only just started to learn programming and things like classes, gets and sets still confuse me, any help would be very much appreciated!