⏱️ 9 min read • Beginner Level • Lesson 34
Method parameters in Java are variables written inside method parentheses. They allow a method to receive input values and perform operations using those values.
Before learning method parameters, you should understand Java Methods, Java Variables, and Java Data Types. After this lesson, continue with Java Method Overloading.
A method parameter is a variable declared in the method definition. It receives a value when the method is called.
static void greet(String name) {
System.out.println("Hello " + name);
}
In the above method, name is a parameter.
Many beginners use parameter and argument as the same word, but technically they are different.
| Term | Meaning | Example |
|---|---|---|
| Parameter | Variable written in method definition. | String name |
| Argument | Actual value passed while calling the method. | "Ayan" |
static void greet(String name) { // name is a parameter
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet("Ayan"); // Ayan is an argument
}
Parameters are written inside the parentheses of a method declaration.
returnType methodName(dataType parameterName) {
// method body
}
For multiple parameters:
returnType methodName(dataType parameter1, dataType parameter2) {
// method body
}
A method can receive one parameter. In this example, the method receives a name and prints a greeting.
public class SingleParameterExample {
static void greet(String name) {
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet("Ayan");
}
}
Output:
Hello Ayan
A method can receive multiple parameters. Each parameter must have its own data type.
public class MultipleParametersExample {
static void showStudent(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
showStudent("Ayan", 20);
}
}
Output:
Name: Ayan Age: 20
A method can have parameters of different data types such as int, double,
String, boolean, and more.
public class DifferentTypeParameters {
static void showProduct(String productName, double price, boolean available) {
System.out.println("Product: " + productName);
System.out.println("Price: " + price);
System.out.println("Available: " + available);
}
public static void main(String[] args) {
showProduct("Laptop", 55000.50, true);
}
}
Output:
Product: Laptop Price: 55000.5 Available: true
Parameters are often used with return type methods. The method receives input values, processes them, and returns a result.
public class ReturnWithParameters {
static int add(int number1, int number2) {
return number1 + number2;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println("Sum: " + result);
}
}
Output:
Sum: 30
Primitive values such as int, double, and boolean are passed by value.
This means the method receives a copy of the value.
public class PrimitiveParameterExample {
static void changeValue(int number) {
number = 100;
System.out.println("Inside method: " + number);
}
public static void main(String[] args) {
int value = 50;
changeValue(value);
System.out.println("Outside method: " + value);
}
}
Output:
Inside method: 100 Outside method: 50
For objects, Java still passes the value of the reference. The method receives a copy of the reference, but both references point to the same object. Therefore, object data can be modified through the parameter.
class Student {
String name;
}
public class ReferenceParameterExample {
static void changeName(Student student) {
student.name = "Rahul";
}
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ayan";
changeName(s1);
System.out.println(s1.name);
}
}
Output:
Rahul
Arrays can be passed as method parameters. This is useful when a method needs to process multiple values.
public class ArrayParameterExample {
static int calculateTotal(int[] marks) {
int total = 0;
for (int mark : marks) {
total += mark;
}
return total;
}
public static void main(String[] args) {
int[] studentMarks = {80, 75, 90};
System.out.println("Total: " + calculateTotal(studentMarks));
}
}
Output:
Total: 245
You can pass an object as a method argument. This allows the method to access object data.
class Employee {
String name;
double salary;
}
public class ObjectParameterExample {
static void printEmployee(Employee employee) {
System.out.println("Name: " + employee.name);
System.out.println("Salary: " + employee.salary);
}
public static void main(String[] args) {
Employee emp = new Employee();
emp.name = "Ayan";
emp.salary = 45000.50;
printEmployee(emp);
}
}
Output:
Name: Ayan Salary: 45000.5
Variable-length arguments, also called varargs, allow a method to accept zero or more values
of the same type. Varargs are written using three dots ....
public class VarargsExample {
static int addNumbers(int... numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
public static void main(String[] args) {
System.out.println(addNumbers(10, 20));
System.out.println(addNumbers(10, 20, 30, 40));
}
}
Output:
30 100
A method parameter can be used only inside the method where it is declared. It cannot be accessed outside that method.
public class ParameterScopeExample {
static void showMessage(String message) {
System.out.println(message);
}
public static void main(String[] args) {
showMessage("Hello Java");
// message cannot be accessed here
}
}
The following example uses different parameter types together: primitive values, strings, arrays, objects, return values, and varargs.
class Student {
String name;
int[] marks;
}
public class MethodParametersCompleteExample {
static int calculateTotal(int... marks) {
int total = 0;
for (int mark : marks) {
total += mark;
}
return total;
}
static void printStudentResult(Student student) {
int total = calculateTotal(student.marks);
System.out.println("Student: " + student.name);
System.out.println("Total Marks: " + total);
}
public static void main(String[] args) {
Student student = new Student();
student.name = "Ayan";
student.marks = new int[] {80, 75, 90};
printStudentResult(student);
}
}
Output:
Student: Ayan Total Marks: 245
Student is an object type used as a method parameter.marks is an array passed to a method.calculateTotal() uses varargs to calculate total marks.printStudentResult() accepts a Student object as parameter.🧠 Test your understanding with a quick quiz
Topic: Method-parameters | Language: Java
public class Test {
static void greet() {
greet("Guest");
}
static void greet(String name) {
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet();
}
}public class Test {
static void change(int number) {
number = 100;
}
public static void main(String[] args) {
int value = 50;
change(value);
System.out.println(value);
}
}public class Test {
static int add(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
public static void main(String[] args) {
System.out.println(add(10, 20, 30));
}
}public class Test {
static void greet(String name) {
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet("Ayan");
}
}🎉 Great job! Continue learning Java step by step.