Definition: A method is a reusable block of code designed to perform a specific task. It can accept parameters, execute code, and optionally return a value.
Syntax of a Method:
returnTypeMethodName(parameterList){
// Method body
}
Example:
publicintAdd(int a,int b){
return a + b;
}
Key Components of a Method:
Return Type: Specifies the type of value the method returns. If a method doesn’t return a value, use void.