Parametrized Method Example in Java
On this page (10sections)
Introduction
Parametrized Method is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
A method (or message) in object-oriented programming (OOP) is a procedure associated with an object class. An object is made up of behaviour and data. Data is represented as properties of the object and behavior as methods. Methods are also the interface an object presents to the outside world. A parameterised method holds variables.
Syntax
access_specifier return_type method_name(data_type variable_name, data_type variable_name){
//Statements
}
Parametrized Method Example Program
public class ParametrizedMethod{
public static void main(String[] args) {
int num1 = 11;
int num2 = 6;
int num3 = minValue(num1, num2);
System.out.println("Minimum Value while comparison of num 1 and num2 is = " + num3);
}
public static int minValue(int i, int j) {
int min;
if (i > j){
min = j;
}
else{
min = i;
}
return min;
}
}
Sample Output
Minimum Value while comparison of num1 and num2 is = 6
When to use
Use this parametrized method example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
int num1 = 11;updates a variable used in the calculation or output. -
int num2 = 6;updates a variable used in the calculation or output. -
int num3 = minValue(num1, num2);updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
The
ifstatement runs the nested code only when the condition is true. -
min = j;updates a variable used in the calculation or output. -
Compare your console output with the sample output for Parametrized Method to confirm the program behaves correctly.
Best Practices
- Name classes in PascalCase and follow one public class per file when starting out.
- Keep
mainshort — delegate work to other methods as programs grow.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.