Methods and Functions in Java
Java Methods
Reusable blocks of code performing specific tasks.
Method Examples
public static int add(int a, int b) {
return a + b;
}
public static void printMessage(String msg) {
System.out.println(msg);
}
public static void main(String[] args) {
int result = add(5, 3);
printMessage("Hello");
}Key Points
- Syntax:
[access] [static] returnType methodName(parameters). staticmethods called without object.- Method overloading: same name, different parameters.
voidmethods don't return value.