Java Add Object to ArrayList error

addarraylistclassjavaobject

I'm new to coding java and would love some help. I'm trying to add an object of the class Fish to an arraylist call fishList

in my main I have for example

    public static void main(String[] args) {

    Fish f1 = new Fish("Nemo");}

and in my class and constructor I have

public class Fish {
protected static String name;
protected static int number;
protected static List<Fish> fistList = new ArrayList<Fish>();


public Fish(String in){
name = in;
number = 15;
fishList.add(name, number);
}

but I get an error "no suitable method found for add(string, int)
method List.add(int, Fish) is not applicable
(actual argument String cannot be converted to int by method invocation conversion)
method List.add(Fish) is not applicable
(actual an formal argument list differ in length)

How do I add objects to an arraylist properly?

Best Answer

First of all name and number shouldn't be static (unless you want all the Fish to have the same name/number but then creating more than 1 instance of that class would be a waste of resources)!

Secondly, change :

fishList.add(name, number);

To:

fishList.add(this);

fishList can hold references to objects of type Fish. If you try to add "name, number" Java doesn't know you mean a Fish :-)

this points to the objects that is currently being created in the constructor.