Moonear
Moonear

Reputation: 23

Java list method declaration?

I am having difficulty understanding this code. I'm unsure if the line that contains a comment is declaring a method or not. I tried googling for list methods but unfortunately didn't find anything. Thank you :)

    List<String> getBrands(String color) {//I don't understand this line of code
        List<String> brands = new ArrayList<String>(); 
        if(color.equals("amber")) {
            brands.add("Jack Amber");
            brands.add("Red Moose");
        } else {
            brands.add("Jail Pale Ale");
            brands.add("Gout Stout");
        }
        return brands;
    }

}

Upvotes: 1

Views: 779

Answers (2)

Necromancer
Necromancer

Reputation: 919

When designing a method, you need to know the following parts.

public static void myMethod(int parameter) throws someException {
   //method body
}
  1. access modifier (public)
  2. optional specifier (static)
  3. return type (void)
  4. method name (myMethod)
  5. parameter list (int parameter)
  6. optional exception (throws someException)
  7. method body ({ // method body })

Note :: access modifier,optional specifier and optional exception are optional. Others are required.

In your code,

 List<String> getBrands(String color) {
    // method body
 }

 /*

 Your access modifier is default (no declaration)
 List<String> is return type
 getBrands is method name
 (String color) is parameter list
 { // .... } is method body

 */

Upvotes: 2

Recips
Recips

Reputation: 46

It's declaring the method of return type List<String>, string being the generic type of the list.

Upvotes: 3

Related Questions