Java Methods
Java Methods
In Java, a method is a set of instructions designed to accomplish a specific task. Methods allow you to reuse code, improve readability, and reduce redundancy by breaking a program into manageable chunks. They can take input, perform operations, and return output.
Types of Java Methods:
Predefined Methods (Built-in Methods):
Java provides built-in methods like Math.sqrt() and System.out.println().
Example:
public class PredefinedMethodExample {
public static void main(String[] args) {
double squareRoot = Math.sqrt(25); // Using a predefined method
System.out.println("Square root: " + squareRoot);
}
}User-defined Methods:
These are written by the programmer to perform custom tasks.
Syntax of a Method:
returnType methodName(parameterList) {
// Body of the method
// Statements to execute
return value; // Optional for methods with a return type
}- Return Type: Specifies the data type of the value that a method returns. Use void if no value is returned.
- methodName: The unique identifier for the method. Follow camelCase naming conventions.
- parameterList: A list of inputs (optional) enclosed in parentheses. Each parameter has a data type and name.
Example: Creating and Using a Method
public class MethodExample {
// User-defined method to calculate the sum of two numbers
int addNumbers(int x, int y) {
return x + y; // Returns the Sum of x + y
}
public static void main(String[] args) {
MethodExample example = new MethodExample(); // Creating an object
int sum = example.addNumbers(10, 20); // Calling the method
System.out.println("Sum: " + sum);
}
}Method Parameters:
Without Parameters: The method does not require input values.
public class NoParameterMethod {
void greet() {
System.out.println("Hello, Welcome!");
}
public static void main(String[] args) {
NoParameterMethod obj = new NoParameterMethod();
obj.greet(); // Calling the method
}
}With Parameters: Input values are passed to the method.
public class ParameterMethod {
void displayMessage(String message) {
System.out.println(message);
}
public static void main(String[] args) {
ParameterMethod obj = new ParameterMethod();
obj.displayMessage("Hello from Java!"); // Passing argument
}
}Method Overloading:
Definition: A single class can have multiple methods with the same name but different parameter lists. This is called method overloading.
Example:
public class MethodOverloadingExample {
// Method with two parameters
int multiply(int a, int b) {
return a * b;
}
// Method with three parameters
int multiply(int a1, int b1, int c1) {
return a1 * b1 * c1;
}
public static void main(String[] args) {
MethodOverloadingExample example = new MethodOverloadingExample();
System.out.println("Product of 7 and 8: " + example.multiply(7, 8));
System.out.println("Product of 7, 8, and 9: " + example.multiply(7, 8, 9));
}
}Return Types:
Method with Return Type: Returns a value to the caller.
public class ReturnTypeExample {
int getNumber() {
return 100; // Returning an integer value
}
public static void main(String[] args) {
ReturnTypeExample example = new ReturnTypeExample();
int result = example.getNumber(); // Storing the returned value
System.out.println("Returned number is: " + result);
}
}Void Method: Does not return any value.
public class VoidMethodExample {
void display() {
System.out.println("This is a void method.");
}
public static void main(String[] args) {
VoidMethodExample example = new VoidMethodExample();
example.display(); // No return value
}
}Static Methods:
Definition: A static method belongs to the class rather than an object and can be called without creating an object of the class.
Example:
public class StaticMethodExample {
static void show() {
System.out.println("Static methods can be invoked without instantiating an object.");
}
public static void main(String[] args) {
show(); // Directly calling the static method
}
}Key Points:
- A method improves code readability and reusability.
- You can call a method multiple times with different arguments.
- Java supports method overloading to provide flexibility.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand JAVA Tutorial visually:
What You'll Learn:
- 📌 #24 Methods in Java
- 📌 Java methods explained in ~10 minutes!