Introduction to Java
METHODS
Prepared by: Jamillah S. Guialil, MIT
Definition, Importance and Syntax:
• A method is a block of code that performs a specific task.
• They allow you to break down complex programs into smaller, more manageable pieces.
• Methods promote code reusability, meaning you can write a piece of code once and call it multiple times.
Importance:
• Code Reusability: Avoid writing the same code multiple times.
• Modularity: Break down complex programs into smaller, manageable units.
• Organization: Improve code readability and maintainability.
• Abstraction: Hide implementation details and focus on what a method does.
(e.g., public, private, protected) Defines the access level of the method .
Specifies the data type of the
Key Components of Method/Syntax:
value returned by the method.
If the method doesn't return a
accessModifier returnType methodName(parameterList) { value, use void.
The block of code // method body
that contains the // statements A unique identifier for the
instructions that the // optional return statement method.
method executes,
enclosed in curly
}
braces {}.
Used to return A comma-separated list of
a value from parameters (input values) that
the method. the method accepts.
Parameters: Return Types:
• Act as input variables for the method. • The data type of the value returned by the method.
• Allow you to pass data into the method. • If the method doesn't return a value, the return type is void.
• The return statement is used to send a value back to the calling code.
• Can be of any data type.
• Formal parameters are the variables defined in the method
declaration.
• Actual parameters are the values passed during the method call.
Example: public int add(int a, int b) {
int sum = a + b;
return sum;
}
public void printMessage(String message) {
[Link](“Hello, world!”);
}
public static void main(String[] args) { //MAIN FUNCTION
Output:
printMessage(); //Calling the printMessage method
Hello, world!
add(5,3); //calling add method with value 8
}
Method Overloading
❑ Method overloading allows you to define multiple methods with the same name but different parameter lists.
❑ The compiler determines which method to call based on the number and types of arguments passed.
❑ This enhances code flexibility and readability. ❑ Key Characteristics of Method Overloading
• Method overloading is a form of compile-time
public int add(int a, int b) { polymorphism (also known as static polymorphism)
which the compiler determines which overloaded method
return a + b;
to call based on the arguments passed during the method
} call.
• Method overloading allows you to use the same method
name for operations that perform similar tasks but with
public double add(double a, double b) { different input data makes your code more intuitive and
return a + b; easier to understand.
} • Method overloading provides the ability to handle
different data types or numbers of inputs without
public int add(int a, int b, int c){ requiring numerous distinct method names.
return a + b + c; ❑ Important Considerations
▪ Methods cannot be overloaded solely based on their
}
return type. The parameter list must differ.
❑ Scope of Variables Within Methods
▪ Variables declared within a method have local scope. ▪ The compiler selects the most appropriate overloaded
▪ They are only accessible within the method's body. method by matching the arguments in the method call
▪ Parameters also have local scope. with the parameter lists of the overloaded methods.
Types of Java Methods
❑ Predefined Methods (Standard Library Methods): These are methods that are already defined in the Java class libraries. Examples include
[Link](), [Link](), and [Link]().
❑ User-defined Methods: These are methods that you create yourself to perform specific tasks in your programs.
❑ Static Methods: Methods that belong to the class itself, and not to any specific instance of the class. They are called using the class name.
public class MyClass {
int instanceVariable = 10;
public void instanceMethod() {
[Link]("Instance method called. Instance variable: " + instanceVariable);
}
public static void staticMethod() {
[Link]("Static method called.");
}
}
❑ Instance Methods: Methods that belong to an instance of a class (an object). They are called using an object reference.
public class Main {
public static void main(String[] args) {
[Link](); // Calling static method
MyClass myObject = new MyClass();
[Link](); // Calling instance method
}
}
❑ Learn more: [Link]