Sitemap
Mayur Ghajare

Mayur Ghajare

B.Tech student at S.B. Jain Institute of Technology, Management & Research, currently specializing in Electronics and Telecommunications Engineering. i

Mastering Java Methods: from Devsync

2 min readJun 9, 2025

--

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?

  1. 🔁 Code Reusability — You can call the same method multiple times.
  2. 📉 Reduces Code Length — Avoid repeating the same block of code.
  3. 📖 Improves Readability — Organizes your logic neatly.

Rules of Method Declaration

  1. Definition — Where you define what the method does.
  2. Calling — Where you actually use the method in your program.
Press enter or click to view image in full size

--

--

Mayur Ghajare
Mayur Ghajare