Adding arrays to methods in classes?

  public static ArrayList<String> removeAnts(ArrayList<String> lunchBox)  

I’m new to coding, and just dived into arrays, just single arrays. Is the removeAnts the name of the method? I know lunchBox is the name of the String array. Just if anyone can explain the line real quick?

Hello @nathangwyn, welcome to the forums! Yes, you are correct, removeAnts in the name of the method.

The public static just create a method with those restrictions (in the case of public, it can be accessed from other classes). The first ArrayList<String> says that the method, removeAnts, will return an ArrayList<String>. Then the parts in parentheses () say that removeAnts takes one argument (lunchBox), of type ArrayList<String>.

I hope this helps!

Can you explain the argument? I know what they are, but I don’t fully understand. The constructer confuses me too, it’s like what happens before an instance of a class?

When you define a function, it can accept arguments. Arguments are ways of passing in different values to a function, to get a different output based on the same process.

An analogy to help

Think about a function as a sausage maker. The meat you put in the top (could be lamb, beef, etc) is like the argument; you can change the type of meat, but you still have to put the same general thing it (you have to use meat; you can’t put, say, aeroplanes in the machine). The output is always going to be a sausage, but, depending on the meat (the argument), you may get different types of sausage.

The constructor method of a class is, as you said, the first method that gets called. When you instantiate a class:

someClass sc = new someClass("some value", "some other value")

The constructor takes the arguments (in this case the strings above), and integrates them into the class. Essentially, a constructor creates an instance of a class, with values specific to that instance.

1 Like

Just teach me everything lol. Do you code for a living?

1 Like

No; its just something I do fun…