- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Methods
In Java, a method is a self-contained block of code designed to perform a specific task. Instead of writing the same logic over and over, you wrap it in a method and "call" it whenever needed. This is the cornerstone of the DRY (Don't Repeat Yourself) principle, which makes your code cleaner, easier to debug, and much more maintainable.
Syntax:
To define a method, you need to specify its return type, a unique name, and any inputs it requires. Here is the basic structure:
accessModifier returnType methodName(parameter1, parameter2, ...) {
// Method body: The logic goes here
// ...
return result; // Required if returnType is not void
}
calculateTotalInventory instead of CalculateTotalInventory). Names should generally start with a verb to describe what the method does.
returnType:
- The return type tells the compiler what kind of data the method will send back to the caller (e.g.,
int,String, or a custom object). - Use void if the method performs an action (like printing to the console) but doesn't need to return any data.
return statement in a method that has a return type other than void. This will cause a compilation error.
methodName:
- The name of the method should be descriptive enough that another developer can understand what it does without reading the code inside.
parameters:
- Parameters act as variables inside the method. They allow you to pass external data into the method to make it dynamic.
- If a method doesn't need data to do its job, the parentheses stay empty:
().
Method Example:
While simple addition is a common example, methods are often used for logic like calculating a discount or formatting a string. Here is a method that calculates a final price after tax:
double calculateTax(double price, double taxRate) {
double taxAmount = price * taxRate;
return price + taxAmount;
}
Calling a Method:
To execute a method, you "call" or "invoke" it by using its name followed by parentheses. If the method requires data, you pass those values (known as arguments) inside the parentheses.
// Calling the method and storing the result in a variable
double total = calculateTax(100.0, 0.08);
System.out.println("Total Price: " + total);
String where an int is expected will break your build.
Void Methods:
- Methods declared with void are used when you want to execute an operation that doesn't produce an output for the rest of the program to use. This is common for logging, updating a database, or printing text.
void displayUserWelcome(String username) {
System.out.println("Welcome back, " + username + "!");
}
Summary
Java methods are the fundamental building blocks of any application. By breaking your program into small, focused methods, you create code that is modular and reusable. Whether you are performing a complex calculation or simply printing a message, mastering methods is the first step toward writing professional-grade Java software.