Mastering Java Methods: from Devsync
Learning to code is not just about writing programs, it’s about writing clean, reusable, and organized code. One of the key concepts in any programming language — including Java — is the Method. If you are learning under @Devsync, this guide will help you understand what methods are, why they matter, and how to use them effectively in Java.
What is a Method?
A method is a block of code grouped together to perform a specific task. It:
- Takes input from the user (parameters)
- Processes that input (statements)
- Returns an output (return types)
A method only executes when it is called, making your code modular and easier to manage.
Types of Methods in Java
There are two main types of methods:
✅ 1. Predefined Methods
These are already available in Java libraries and do not require us to define their functionality.
Examples include:print(); // to print output
sort(); // to sort data
sqrt(); // to calculate square root
nextInt(); // to take integer input from the user
2. User-Defined Methods
Created by the programmer to perform specific custom tasks.
Examples:add(); // to add numbers
sub(); // to subtract numbers
m1(); // generic function
show(); // to display output
Get Mayur Ghajare’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax of a Method:
returnType functionName(parameters){
// statements
}
Example:
int add(int a, int b){
return a + b;
}
Why Use Methods?
- 🔁 Code Reusability — You can call the same method multiple times.
- 📉 Reduces Code Length — Avoid repeating the same block of code.
- 📖 Improves Readability — Organizes your logic neatly.
Rules of Method Declaration
- Definition — Where you define what the method does.
- Calling — Where you actually use the method in your program.





