Java Method
In Java, a method is a block where we define the functionality or behavior of an object, and it is defined within a class.
It perform some specific task and also we call it as function.
The set of logic that is defined once and can be reused multiple times whenever required. Therefore, it helps improve code reusability and maintainability.
A method has a name, and it can be called using that name to execute the logic inside.
Methods can take inputs (parameters) and return outputs (return values).
How to define Method:
Syntax:
accessModifier returnType methodName(parameterList) {
// method body
return value; //returns some value or void;
}Explanation:
- accessModifier It controls the scope or visibility of the method (Example: public, private, protected, or default).
- returnType After successfully execution it returns some value or void if no value is returned.
- methodName It is the name used to identify and call the method.
- parameterList It contains the optional input values passed to the method.
- method body It contains the statements or logic that execute when the method is called.
- return statement sends a value back to the caller and is optional for void methods, we can say it is the output/ result of the method.
Method Signature:
Method signature is the combination of only method name and parameter lists.
Method Without Return value:
Some methods executes some tasks but do not return any value. In these cases, we use the void keyword in the method signature which indicates that the method does not return any value. We also call it as void method.
Syntax:
void methodName(parameterType1, parameterType2, ...){
// method body
}
Example:
public class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myMethod(); // output: Hello!
}
}
class MyClass {
// Method having no return value (void method)
public void myMethod() {
System.out.println("Hello!");
}
}Method with return value:
Some methods executes some tasks and return some value. In these cases, we mention the returnType of the output/result. This return type indicates that the method returns a value of that type.
Example:
public class HelloWorld {
public static void main(String[] args) {
Calculator obj = new Calculator();
int result = obj.add(5, 3);
System.out.println("Sum: " + result);// output: 8
}
}
class Calculator {
//Method having return value of type int
int add(int a, int b) {
return a + b;
}
}Method With Parameters:
Some methods take input values to perform their tasks. These input values are called parameters or arguments.
Example:
public class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myMethod("Ashish"); // output: Hello Ashish
}
}
class MyClass {
//Method having input parameter of type String
void myMethod(String name) {
System.out.println("Hello, " + name + "!");
}
}Method Without Parameters:
Some methods do not require any input values to perform their tasks. These methods are defined without any parameters.
Example:
public class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myMethod(); // output: Hello everyone....
}
}
class MyClass {
//Method with no input parameters
void myMethod() {
System.out.println("Hello everyone....");
}
}Method parameters or arguments:
Example:
public class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
//Here 5 and 3 are Actual Arguments
int result = obj.myMethod(5, 3);
System.out.println("Addition of two number result :" + result); // output: 8
}
}
class MyClass {
//Here number1 and number2 are Formal Arguments
int myMethod(int number1, int number2) {
System.out.println("Add two numbers");
return number1 + number2;
}
}The arguments defined in the method definition are called formal arguments.
The arguments used to call the method are called actual arguments.
Instance method
When we define a method inside the class with out static keyword , that become instance method.
It is instance method because to access or call or execute this method we need to create an object of the class.
So instance method is associated with the object or instance.
Example:
public class HelloWorld {
public static void main(String[] args) {
// creating object of HelloWorld class
MyClass obj = new MyClass();
// calling instance method using object
obj.myInstanceMethod();
}
}
class MyClass {
// instance method
public void myInstanceMethod() {
System.out.println("Hello, World!");
}
}
Static Method:
When we define a method inside the class with static keyword , that become static method.
We can access or call or execute this method by using the class name, as it is associates with the class.
We can use this to create utility methods which we can call directly with the class name without creating the object.
Example:
public class HelloWorld {
public static void main(String[] args) {
// calling static method using class name
MyClass.welcomeMessage();
}
}
class MyClass{
// static method
static void welcomeMessage() {
System.out.println("Hello from static method!");
}
}Method Overloading:
Two or more than two method can have same name inside same class is called method overloading. But methods parameters must be different. That is parameter must be different with respect to number or parameters, type of parameters, order of parameters.
Example-1: Number of parameters are different:
public class HelloWorld {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.show(); // output : Method with no parameters
myClass.show("Rahul"); // output : Method with 1 parameters
myClass.show("Anita", 20); // output : Method with 2 parameters
myClass.show("Kiran", 22, "Bangalore"); // output : Method with 3 parameters
}
}
class MyClass {
// Method 1: No parameter
void show() {
System.out.println("Method with no parameters");
}
// Method 2: One parameter
void show(String name) {
System.out.println("Method with 1 parameters");
}
// Method 3: Two parameters
void show(String name, int age) {
System.out.println("Method with 2 parameters");
}
// Method 4: Three parameters
void show(String name, int age, String city) {
System.out.println("Method with 3 parameters");
}
//So here method is overloaded with same name but different parameters.
}Example-2: Type of parameters are different:
public class HelloWorld {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.show(10); // output : Method with int parameter
myClass.show(10.7); // output : Method double parameter
myClass.show("Ashish"); // output : Method string parameter
}
}
class MyClass {
// Method 1: int parameter
void show(int a) {
System.out.println("Method with int parameter");
}
// Method 2: double parameter
void show(double a) {
System.out.println("Method with double parameter");
}
// Method 3: String parameter
void show(String a) {
System.out.println("Method with string parameter");
}
// So here method is overloaded with same name but different types of parameters.
}
Example-3: Order of parameters are different:
public class HelloWorld {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.show(10, "Ashish"); // output : Method with parameter order : int , string
myClass.show("Ashish", 10); // output : Method with parameter order : string , int
}
}
class MyClass {
// Method 1: int first, String second
void show(int a, String b) {
System.out.println("Method with parameter order : int , string");
}
// Method 2: String first, int second (order changed)
void show(String a, int b) {
System.out.println("Method with parameter order : string , int");
}
// So here method is overloaded with same name but different order of parameters.
}Example-4: Number of parameters, order of parameters and type of parameters are different:
public class HelloWorld {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.show(); // Method with no parameters
myClass.show("Ashish"); // Method with 1 parameters (String)
myClass.show(10.5); // Method with double parameter
myClass.show(10, "Ashish"); // Method with parameter order : int , string
myClass.show("Ashish", 10); // Method with 2 parameters
myClass.show("Ashish", 10, "BLR"); // Method with 3 parameters
}
}
class MyClass {
void show() {
System.out.println("Method with no parameters");
}
// Method 2: One parameter
void show(String name) {
System.out.println("Method with 1 parameters");
}
// Method 3: Two parameters
void show(String name, int age) {
System.out.println("Method with 2 parameters");
}
// Method 4: Three parameters
void show(String name, int age, String city) {
System.out.println("Method with 3 parameters");
}
// Method 2: double parameter
void show(double a) {
System.out.println("Method with double parameter");
}
// Method 3: String parameter
void show(String a) {
System.out.println("Method with string parameter");
}
// Method 2: int first, string second (order changed)
void show(int a, String b) {
System.out.println("Method with parameter order : int , string");
}
// So here method is overloaded with same name but different order, types, and number of parameters.
}How method overloading works in Java:
So here When you call an overloaded method, the Java compiler decides which method to execute before the program runs. There is some rules how java decides which method to call or invoke if multiple overloaded methods are available.
First it tries Exact match of method arguments.
If not found, it follows type promotion (Example - int → long → double)
If still ambiguous, it gives a compile-time error
Here is 3 different examples:
Example-1 (First it tries Exact match of method arguments.)
class Demo {
// Method 1: takes int
void show(int a) {
System.out.println("Method with int: " + a);
}
// Method 2: takes long
void show(long a) {
System.out.println("Method with long: " + a);
}
public static void main(String[] args) {
Demo obj = new Demo();
// ---------------- Example 1: Exact match ----------------
//Here during method call the actual argument is int, and there is a method with int parameter available.
// So Java selects show(int a) directly (no conversion needed).
obj.show(10);
}
}Example-2 (it follows type promotion (Example - int → long → double))
class Demo {
// Method 1: takes long
void show(long a) {
System.out.println("Method with long: " + a);
}
// Method 2: takes double
void show(double a) {
System.out.println("Method with double: " + a);
}
public static void main(String[] args) {
Demo obj = new Demo();
// ---------------- Example 2: Type Promotion ----------------
// Here during method call the actual argument is int, and there is no method with int parameter available.
// So Java applies type promotion:
// int → long (preferred first), so it will find the method with long parameter.
// If needed further → long → double
obj.show(10);
// Output: Method with long: 10
// Reason: int is promoted to long and matched with show(long)
}
}Example-3 (If still ambiguous, it gives a compile-time error)
class Test {
void show(int a, double b) {
System.out.println("int-double version");
}
void show(double a, int b) {
System.out.println("double-int version");
}
public static void main(String[] args) {
Test obj = new Test();
obj.show(10, 10); // ❌ Ambiguity error
}
}So here
show(int, double) → second argument can be widened (int → double)
show(double, int) → first argument can be widened (int → double)
So the compiler cannot decide which one is a better match.
See you in next page....🙂