- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Method Calls in Java
Java provides different method calls techniques that we can use according to the need andscenario in our program. Here, methods mean a block of code that can be reused multipletimes to perform a single operation. It saves our time and also reduces the size of code.The method call is referred to as invocation of methods. To use the functionality of amethod, it must be invoked by some means. This article aims to explain how methods arecalled in Java
User-defined Method in Java
Before discussing the method call, let’s familiarize ourselves with syntax of the userdefined methods
Syntax
accessSpecifier nonAccessModifier return_Type method_Name(parameters) { // Body of the method }
Here,
accessSpecifier − It is used to set the accessibility of the method. It may be public,protected, default and private.
nonAccessModifier − It shows additional functionalities or behavior of the method such as static and final.
return_Type − The datatype a method is going to return. We use the void keyword when method does not return anything
parameters − act as a placeholder for arguments
Approach 1: Calling with an Argument
Arguments − are the actual values that are passed during the time of method call. They must match the type of parameter passed in the method definition
Example
In this example, we will define a parameterized static method that will accept a parameterof type integer and perform a multiplication operation. We call this method using anargument in the main() method of same class.
public class Call1 { static void methodA(int data1) { data1++; int data2 = 5; int mult = data1 * data2; System.out.println("Value of data1 and data2 multiplication is: " + mult); } public static void main(String []args) { int data1 = 3; methodA(data1); // calling method with argument } }
Output
Value of data1 and data2 multiplication is: 20
Approach 2: Calling without an Argument
Example
In this example, we will define a static method to perform a multiplication operation. Sinceit does not accept parameters, we call this method without any argument in the main()method of same class.
public class Call2 { static void methodA() { int data1 = 3; data1++; int data2 = 5; int mult = data1 * data2; System.out.println("Value of data1 and data2 multiplication is: " + mult); } public static void main(String []args) { methodA(); // calling method without argument } }
Output
Value of data1 and data2 multiplication is: 20
Approach 3: Method Calling using Object
Example
In this example, we will define a non-static method to perform a multiplication operation. Since a non-static method is called using an object. Therefore, we need to create theobject of class to call the ‘methodA()’.
public class Call3 { public void methodA() { int data1 = 3; data1++; int data2 = 5; int mult = data1 * data2; System.out.println("Value of data1 and data2 multiplication is: " + mult); } public static void main(String []args) { Call3 obj = new Call3(); // creating an object obj.methodA(); // calling methodA using object } }
Output
Value of data1 and data2 multiplication is: 20
Approach 4: Method Calling using Class name
Example
To call a static method of a class inside another class, we use the name of class along withthe period operator. In this example, we will call the in-built static method of class ‘Math’.
public class Call4 { public static void main( String[] args ) { double x = 6.55; double y = 4.32; System.out.println(" Ceil value of x: " + Math.ceil(x) ); System.out.println(" Floor value of y: " + Math.floor(y) ); System.out.println(" Value of PI = " + Math.PI); } }
Output
Ceil value of x: 7.0 Floor value of y: 4.0 Value of PI = 3.141592653589793
Conclusion
In Java, method calling is quite simple and straightforward. We have discussed various ways to call a method in detail using example programs. Also, we discovered the syntax to create a user-defined method.