☰ See All Chapters |
Java Method - How to Create Method in Java
Java method is a collection of statements that are grouped together to perform an operation. Methods represent the behaviors of the object. Methods can take input in the form of parameters, performs the operation on input and returns a result to the caller. A method will be executed by a call to the method.
Creating a Method
Method has the following syntax (method definition)
Access_specifier Non_Access_specifier returntype methodName (type1 pmtr1, type2 pmtr2, type3 pmtr3..) { //Method body return exp;|return; } |
Access specifier: The possible values of the access specifier are public, private, protected, or default access
Non_Access_specifier: The possible values for these specifiers are static, final, abstract, native, and synchronized. A method might not use any of these specifiers, or a method might use more than one of them.
The order of the access specifiers and optional non access specifiers is arbitrary. However, you will almost always see the access specifier appear first in a method signature because it is the preferred style among Java programmers.
Non_Access_specifier Access_specifier returntype methodName (type1 pmtr1, type2 pmtr2, type3 pmtr3..) { //Method body return exp;|return; } |
returnType: A method may return a value. The return value Type is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the return value Type is the keyword void. Methods that have a return type other than void return a value to the calling routine using the following form of the return statement:
return exp;
Here, value is the value returned.
Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature. This can be any legal identifier other than those already used by other items within the current scope.
Parameters:
(type1 pmtr1, type2 pmtr2, type3 pmtr3..)→ Parameters
Where pmtr: Parameter, type: data type
The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty.
Method Body: The method body contains a collection of statements that define what the method does.
Note: In certain other languages, methods are referred to as procedures and functions. A method with a nonvoid return value type is called a function; a method with a void return value type is called a procedure.
Syntax to call method
variable = objectReference.methodName(arg1, arg2, arg3…)
Method may be/may not be returning a value, accepting the value while calling a method is optional. We can ignore the return value from method call.
If method is not returning a value then syntax to call the method should be:
objectReference.methodName(arg1, arg2, arg3…)
The data type of variable which receives the return value should be same as the returntype of a method.
Data type of the arguments should match with the data type of the parameter in order. Data type of argument arg1 should match with the data type of parameter pmtr1. Data type of argument arg2 should match with the data type of parameter pmtr2. Data type of argument arg3 should match with the data type of parameter pmtr3 So on…
Number of arguments should be same as number of parameters.
What is method signature?
A method signature consists only of a method’s name and parameter list. The modifiers, return type, exception list, and method body are not considered a part of a method’s signature.
What is method binding?
Connecting method call to method body is known as method binding.
What is the difference between call by value and call by reference?
In java there is no concept of call by reference, because even if we call a method by passing reference variable as an argument, still the reference variable will be having hexadecimal memory address of the object. And this hexadecimal memory address is again a value. So all the method calls are through call by value.
Method Chaining
Auto Type casting of parameters in Method call
When a method is called compiler compares the arguments with the parameters and the method which is matched will be called. Suppose if no matching method is found then compiler will type cast the parameters to their parent type and check for the match.
In the below example when demo.meth(20, 20) is called, 20 is an integer value, there are no method matching the parameters. Now compiler type cast the value 20 to long type and then when matching is found, matched method will be invoked.
public class MethodOverloadingDemo {
public static void main(String[] args) {
System.out.println("www.java4coding.com"); MethodOverloadingDemo demo = new MethodOverloadingDemo(); demo.meth(20, 20);
}
public void meth(long a, int b) { System.out.println("Method overloading by changing the data type of parameters"); }
} |
Method Call Stack
A method is invoked (also referred to as called), which causes flow of control to jump to the method that is being invoked. Flow of control then executes the statements within the method. Of course, the method being executed might invoke another method, causing flow of control to jump to this other method. All method calls are maintained in a structure known as the call stack. The current method that is executing is at the
top of the call stack. When this method completes executing, it is removed from the top of the call stack, and the flow of control returns to the previous method on the stack. When a new method is invoked, this new method gets placed at the top of the call stack.
The first method that is invoked in your Java program is main (), which is invoked by the JVM. Therefore, main () is at the bottom of your method call stack.
A Java program can have more than one call stack if it is a multithreaded application.
All Chapters