What is the syntax to define a method in Java? Provide an example.
In Java, a method is defined using the following syntax:
modifier returnType methodName(parameterList) {
// method body
}
Explanation:
Modifier: Defines the access level of the method (e.g., public, private, protected).
Return Type: Specifies the type of value the method returns (e.g., int, void for no return).
Method Name: Identifies the method; it should be descriptive and follow camelCase.
Parameter List: Declares parameters that the method accepts (optional).
Method Body: Contains the code to be executed when the method is called.