-1

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!

nandhp
  • 4,680
  • 2
  • 30
  • 42
Ser Davos
  • 25
  • 1
  • 2
  • 6

3 Answers3

1

printAllFish is non-static method and you are trying to call it in a static way i.e. using the class name and not the instance.

You should call it using one of the instance i.e. f1 or f2:

f1.printAllFish();
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • If I make printAllFish static, these lines are not static and throw up the same error; int size = this.getFishNumOf();, String a = this.getName();, int b = getAge();. Thanks for the reply friend – Ser Davos Oct 22 '13 at 06:46
  • @SerDavos static methods cannot access non-static fields. This is because static methods can be called without creating an instance. And if instance is not created then it does make sense to access intance fields. – Juned Ahsan Oct 22 '13 at 06:52
  • All fixed now thanks Juned! – Ser Davos Oct 22 '13 at 06:53
  • @SerDavos If you want to access non-static/instance fields in printAllFish then keep that method as non-static only. And simply call it using any of the instances. – Juned Ahsan Oct 22 '13 at 06:53
1

You need to make printAllFish, getFishNumOf and fishList static and remove the this keyword before getFishNumOf. Then in the for loop you have to specify which fish you're getting the name and number of for each iteration of the loop. For instance:

for(Fish f : fishList)
    String a = f.getName();
    int b = f.getNumber();
    System.out.println("The Fish " + a + " is number " + b );
}
MrAzzaman
  • 4,734
  • 12
  • 25
  • Thank you so much MrAzzaman, you really helped me out! I will implement you for loop as it is much nicer :) – Ser Davos Oct 22 '13 at 06:51
  • @Ser Davos - Also try to figure out/learn where and how to use static keyword, and every reason behind the solution provided – Gnanz Oct 22 '13 at 07:43
0

you have to call your initialized object method. not Fish.printAllFish() but f1.printAllFish(), f2.printAllFish() or m.printAllFish()

I suggest using a different class named FishContainer where it has a List object of Fish objects and when initializing a Fish object, you add it to FishContainer's list property and then call the printAllFish() from the container class. Try sth like this:

FishContainer container = new FishContainer();
Fish f1 = new Fish("Nemo");
Fish f2 = new Fish("Dory");
f2.setNumber(2);
Fish m = new Fish("Bruce");
m.setNumber(3);

container.addFish(f1);
container.addFish(f2);
container.addFish(m);
container.printAllFish();
Apostolos
  • 10,033
  • 5
  • 24
  • 39