Please enable JavaScript.
Coggle requires JavaScript to display documents.
Defining methods() - Coggle Diagram
Defining methods()
(In Java, a method definition has four basic parts+
2 optional part)
-
-
-
-
a modifier, such as public or private,
throws keyword, which indicates the exceptions a method can throw.
basic method definition
returnType methodName(type1 arg1, type2 arg2, type3 arg3 ...)
{
// body of method
}
The method’s parameter list is a set of variable declarations separated by commas and placed inside parentheses. These parameters become local variables in the body of the method, receiving their values when the method is called.
returnType is the primitive type or class of the value returned by the method (or void if the method does not return a value).
If a method returns an array object, the array brackets can go after either the return type or the closing parenthesis of the parameter list. Because having the brackets after the return type is easier to read,
int[] makeRange(int lower, int upper)
{
// body of method
}
-
-
return type
Unless a method has been declared with void as its return type, the method returns some kind of value when it is completed.
This value must be explicitly returned at some exit point inside the method by using the return keyword.