Method parameters

The parameter list of a method within parentheses is followed immediately after the method’s name. A method can have zero or more parameters and if a method has zero parameters the parentheses are empty, they cannot be omitted. A method parameter is declared with its data type followed by its name. When the method is called, the values passed to that method needs to be of exactly the same type as in the declaration of the method.

Here are some example method declarations:

Continue reading

Methods with variable-length argument list (varargs)

A method with a variable length parameter list has the ability to take different number of arguments.
These methods are declared using three dots after the data type of the last argument in the list. Only one parameter in the list may be declared as a varargs parameter and that parameter has to be the last one in the list.

Continue reading

Method signature with checked exception

A method signature can end with a throws clause followed by one or more exception types that the method can throw. A ‘checked exception’ is an exception that is derived from the class (or is in fact an instance of) Exception.
A checked exception is not mandatory but should be declared in a method signature in the following cases:

Continue reading