What is a Method?
A method is a block of code that performs a specific task. We can create a method once and call it whenever required.
Methods help us to reuse code and avoid repetition.
How to Create a Method?
Syntax:
accessModifier returnType methodName(parameters) {
// Method body
}
Example:
public class Main {
static void greet() {
System.out.println("Hello World");
}
public static void main(String[] args) {
greet();
}
}
Types of Methods
1) Predefined Methods
Methods that are already available in Java libraries.
String name = "java";
System.out.println(name.toUpperCase());
System.out.println(name.length());
2) User-Defined Methods
Methods created by programmers to perform specific tasks.
static void display() {
System.out.println("Learning Java");
}
Parameters and Arguments
- Parameters: Variables declared in the method definition.
- Arguments: Actual values passed when calling the method.
static void add(int a, int b) {
System.out.println(a + b);
}
add(10, 20);
Here, a and b are parameters, whereas 10 and 20 are arguments.
Return Type
A return type specifies the type of value a method returns.
static int add(int a, int b) {
return a + b;
}
Here, int is the return type, and the return keyword sends the result back to the calling method.
Void Method
When a method doesn't return any value, we use the void keyword.
static void display() {
System.out.println("Hello Java");
}
Static and Instance Methods
1) Static Method
A static method belongs to the class. We can call it without creating an object.
static void display() {
System.out.println("Static Method");
}
Main.display();
2) Instance Method
An instance method belongs to an object. We need to create an object to call it.
void display() {
System.out.println("Instance Method");
}
Main obj = new Main();
obj.display();
Method Overloading
Method overloading means creating multiple methods with the same name but different parameters.
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
We can overload methods by changing the number, type or order of parameters.
Note: Changing only the return type is not method overloading.
Method Signature
A method signature consists of the method name and parameter types.
Example:
static int add(int a, int b)
Method signature: add(int, int)
Varargs
Varargs (Variable Arguments) allows a method to accept multiple arguments.
We use three dots (...) to declare it.
static void display(int... numbers) {
for (int n : numbers) {
System.out.println(n);
}
}
Recursion
Recursion is a process in which a method calls itself.
static int factorial(int n) {
if (n == 1)
return 1;
return n * factorial(n - 1);
}
Here, the method calls itself until the base condition is reached.
Pass by Value
Java always follows pass by value. It means a method receives a copy of the value passed to it.
static void change(int number) {
number = 100;
}
public static void main(String[] args) {
int value = 50;
change(value);
System.out.println(value);
}
Output:
50
Changing the copied value inside the method doesn't change the original variable.
Important Points
- Methods help us reuse code.
-
voidmethods don't return values. - Static methods belong to classes.
- Instance methods belong to objects.
- Method overloading allows the same method name with different parameters.
- Java always follows pass by value.
- Recursion means a method calling itself.
Top comments (0)