Methods in Java
Methods (also called functions) are blocks of code that perform specific tasks and can be reused throughout your program.
Method Structure
MethodStructure.java
public class MethodStructure {
// Method signature: return_type methodName(parameters)
// Simple method with no parameters and no return value
public static void sayHello() {
System.out.println("Hello, World!");
}
// Method with parameters
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
// Method with parameters and return value
public static int add(int a, int b) {
return a + b;
}
// Method with multiple parameters of different types
public static void printPersonInfo(String name, int age, boolean isStudent) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Student: " + isStudent);
}
public static void main(String[] args) {
// Calling methods
sayHello();
greet("John");
int sum = add(5, 3);
System.out.println("Sum: " + sum);
printPersonInfo("Alice", 25, true);
}
}Method Overloading
Java allows multiple methods with the same name but different parameters:
MethodOverloading.java
public class MethodOverloading {
// Method with int parameter
public static void display(int number) {
System.out.println("Integer: " + number);
}
// Method with double parameter
public static void display(double number) {
System.out.println("Double: " + number);
}
// Method with String parameter
public static void display(String text) {
System.out.println("String: " + text);
}
// Method with multiple parameters
public static void display(String text, int number) {
System.out.println(text + ": " + number);
}
public static void main(String[] args) {
display(100);
display(99.99);
display("Hello");
display("Age", 25);
}
}Variable Arguments (Varargs)
VarargsExample.java
public class VarargsExample {
// Method with variable arguments
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
// Method with fixed parameters plus varargs
public static void printDetails(String name, int... scores) {
System.out.println("Student: " + name);
System.out.println("Scores: ");
for (int score : scores) {
System.out.println(" " + score);
}
}
public static void main(String[] args) {
// Can call with any number of arguments
System.out.println("Sum of 2 numbers: " + sum(10, 20));
System.out.println("Sum of 4 numbers: " + sum(10, 20, 30, 40));
System.out.println("Sum of 1 number: " + sum(100));
printDetails("John", 85, 92, 78);
}
}Recursive Methods
RecursiveExample.java
public class RecursiveExample {
// Factorial calculation using recursion
public static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// Fibonacci sequence
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String[] args) {
System.out.println("Factorial of 5: " + factorial(5));
System.out.println("Factorial of 7: " + factorial(7));
System.out.println("Fibonacci of 6: " + fibonacci(6));
System.out.println("Fibonacci of 8: " + fibonacci(8));
}
}Static vs Instance Methods
StaticVsInstance.java
public class StaticVsInstance {
// Static method (belongs to class)
public static void staticMethod() {
System.out.println("This is a static method");
}
// Instance method (belongs to object)
public void instanceMethod() {
System.out.println("This is an instance method");
}
public static void main(String[] args) {
// Call static method directly
staticMethod();
// Create object to call instance method
StaticVsInstance obj = new StaticVsInstance();
obj.instanceMethod();
}
}ℹ️
Static methods can be called without creating an object, while instance methods require object creation.
Learn about classes and objects to understand object-oriented programming in Java.
Last updated on