3. Methods in Java
Methods are a fundamental part of Java programming, enabling code reusability, modularity, and organization. They are blocks of code that perform a specific task and can be called upon when needed.
1. What is a Method?
A method is a collection of statements that are grouped together to perform an operation. They can take inputs, perform operations, and return a result.
2. Method Syntax
The general syntax for defining a method in Java is:
returnType methodName(parameterType parameterName) {
// Method body
}
Example:
public int add(int a, int b) {
return a + b;
}
3. Types of Methods
3.1. Standard Methods
-
Instance Methods: Belong to an instance of a class. They can access instance variables and other instance methods.
public class Calculator {
public int multiply(int x, int y) {
return x * y;
}
} -
Static Methods: Belong to the class itself rather than any specific instance. They cannot access instance variables.
public class MathUtil {
public static int square(int num) {
return num * num;
}
}
3.2. Constructor Methods
-
Constructors: Special methods used to initialize objects. They have the same name as the class and do not have a return type.
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
3.3. Overloaded Methods
-
Method Overloading: Allows defining multiple methods with the same name but different parameter types or counts.
public class Display {
public void show(int number) {
System.out.println("Number: " + number);
}
public void show(String text) {
System.out.println("Text: " + text);
}
}
3.4. Overridden Methods
-
Method Overriding: Allows a subclass to provide a specific implementation for a method already defined in its superclass.
public class Animal {
public void sound() {
System.out.println("Animal sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}
4. Method Parameters
Methods can accept parameters, which can be:
- Formal Parameters: Variables in the method definition.
- Actual Parameters: Values passed to the method when it is called.
Example:
public void greet(String name) {
System.out.println("Hello, " + name);
}
// Calling the method
greet("Alice"); // "Hello, Alice"
5. Return Statement
A method can return a value using the return statement. If a method has a return type other than void, it must return a value.
Example:
public int sum(int a, int b) {
return a + b;
}
6. Varargs (Variable Arguments)
Java allows you to pass a variable number of arguments to a method using varargs.
Example:
public void printNumbers(int... numbers) {
for (int number : numbers) {
System.out.println(number);
}
}
// Calling the method
printNumbers(1, 2, 3, 4); // Prints 1 2 3 4
7. Summary of Methods
| Feature | Description |
|---|---|
| Definition | Block of code performing a specific task. |
| Return Types | Can return a value or be void (no return). |
| Parameters | Can accept inputs; can be required or optional (varargs). |
| Overloading | Multiple methods with the same name but different signatures. |
| Overriding | Subclass provides a specific implementation of a superclass method. |
| Static vs. Instance | Static methods belong to the class; instance methods belong to an object. |