Consider the following method declaration in Java:
public class MathUtils {
public static int multiply(int a, int b) {
return a * b;
}
}
public
keyword in the method declaration.static
keyword in the method declaration.multiply
method in the MathUtils
class with the values 5
and 3
. Store the result in a variable called product
.product
after the invocation?The public
keyword in the method declaration allows the method to be accessible from anywhere in the program. This means that other classes in different packages can access and use this method.
The static
keyword in the method declaration makes the method belong to the class itself rather than to an instance of the class. This means that the method can be called using the class name (MathUtils.multiply()
) instead of creating an object of the class.
To invoke the multiply
method in the MathUtils
class with the values 5
and 3
, you can use the following code:
int product = MathUtils.multiply(5, 3);
The steps involved in the method invocation process are as follows:
MathUtils.multiply()
).(5, 3)
are passed to the method.The value of product
after the invocation would be 15
, as the multiply
method multiplies 5
and 3
.