Lecture Notes on Methods in Java
1. Introduction to Methods in Java
Definition of a Method
A method in Java is a block of code that performs a specific task. It helps in organizing the
code by breaking it into reusable modules.
Key Features of Methods
- Code Reusability: Write once and call multiple times.
- Modularity: Helps in dividing complex problems into smaller, manageable parts.
- Readability: Makes code easier to understand.
- Maintainability: Reduces redundancy, making modifications easier.
2. Declaring and Defining Methods in Java
Syntax of a Method
returnType methodName(parameter1, parameter2, ...) {
// Method body
return value; // (if returnType is not void)
}
Example: A Simple Method
public class Example {
static void greet() {
[Link]("Hello, welcome to Java!");
}
public static void main(String[] args) {
greet(); // Calling the method
}
}
3. Types of Methods in Java
1. Predefined Methods (Built-in Methods)
[Link]("Hello"); // println() is a predefined method
[Link](5, 10); // max() method from Math class
2. User-Defined Methods
public class Calculator {
int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
int sum = [Link](5, 10);
[Link]("Sum: " + sum);
}
}
4. Method Parameters and Return Types
Passing Parameters to Methods
void displayMessage(String message) {
[Link](message);
}
Returning Values from Methods
int square(int num) {
return num * num;
}
Example: Method with Parameters and Return Type
public class MathOperations {
int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
MathOperations math = new MathOperations();
int result = [Link](4, 5);
[Link]("Multiplication Result: " + result);
}
}
5. Method Overloading
class OverloadExample {
void display(int a) {
[Link]("Integer: " + a);
}
void display(double b) {
[Link]("Double: " + b);
}
public static void main(String[] args) {
OverloadExample obj = new OverloadExample();
[Link](10);
[Link](5.5);
}
}
6. Static and Instance Methods
Static Methods
class StaticExample {
static void greet() {
[Link]("Hello from a static method");
}
public static void main(String[] args) {
greet();
}
}
Instance Methods
class InstanceExample {
void show() {
[Link]("Hello from an instance method");
}
public static void main(String[] args) {
InstanceExample obj = new InstanceExample();
[Link]();
}
}
7. Recursion in Java
class RecursionExample {
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
RecursionExample obj = new RecursionExample();
[Link]("Factorial of 5: " + [Link](5));
}
}
8. Conclusion
Methods promote code reuse and modularity. Java supports predefined, user-defined,
overloaded, static, and recursive methods. Methods can have parameters and return values
to enhance functionality.
Recommended Reading
- Java Programming, Second Edition - Joyce Farrell
- JAVA 2 Programming Black Book - Holzner Steven
- JAVA Programming - Wigglesworth and Lumby