How to Call a Method in Java with Code Example

In this tutorial, we will learn how to call method in Java with the help of an example. Java is an object-oriented language, so its methods must be defined in a class.

Moreover, if a method is declared as a class, it can be called from the main method or any other method. In Java libraries, there are also certain built-in methods. Using the syntax listed below, we can call any built-in or self-defined method.

What is a Method in Java?

In the Java programming language, a “method” is a piece of code that implements a different function and is only executed when it is called.

In addition, methods are also generally called as functions. In every method it has a name. Parameters allow us to pass information into a method. The method also has a return type through specifying the type of data that it will return.

In other words, the name of the method needs to be written in lower CamelCase. And the first letter will be small. Also, a method must always be given an appropriate name. Rather, a verb will refer to what it does. For example: add(), printContactList(), updateInfo() etc.

In every time a program meets a method call, the program execution switches to the methods in program code. The body of the program will be executed, and the method returns to the previous program code from where it was called and continues on the following line. When a method returns to the program code that will be called.

  1. It will finalize all the program code in the method and need to reach the end of it.
  2. It will get to the return statement.
  3. It will return an exception.

Why are Methods used?

The methods are used because they authorized the code to used again without editing it again and again. In addition, methods can save time and manage the code in an organized and readable. It’s used for customization of the program. When the methods do not use, the program will become unusually formal and very difficult to test, debug or sustain the code.

Creating a Method in Java

public class Driver {

	public static void printProgrammingLanguage(String language) {

		System.out.println("Hi, I am " + language + "!");
	}

Declaration of a Method in Java

Here are the components of method declaration

  • The Modifier: it defines the access type in which the method will be able to access the program, for example: private and public. So, in this case, it will be in a public, which means in this method it will be able to accessed outside of the class.
  • The Return Type: It is the data type of the value returned by the method. In this example, it is void, which means it returns nothing or null.
  • The Method Name: It is the name of the method in our program that will be used to call it. The name of the method is printProgrammingLanguage.
  • The Parameter List: This consists the list of data which need to be acknowledged in the method. In every input data is separated by commas and is followed by the datatype. When there is no data to be confirmed into the brackets () which are left empty. We shall pass one parameter name of type string.
  • The Method Body: It contains a program code which needs to run within curly braces {}.

Call Method in Java

For calling a method in Java. Enter the method’s name in parenthesis(), followed by a semicolon (;). When the method declaration provides parameters. Parenthesis () are used to pass those parameters, however, in instances their datatypes are not defined. It is essential to follow the argument sequence provided in the method declaration.

For example:

public class Driver {

	public static void printProgrammingLanguage(String language) {

		System.out.println("Hi, I am " + language + "!");
	}

	public static void main(String[] args) {

		String language = "PHP LANGUAGE";
		printProgrammingLanguage(language);

		String language1 = "JAVA LANGUAGE";
		printProgrammingLanguage(language1);

		String language2 = "JAVACRIPT LANGUAGE";
		printProgrammingLanguage(language2);

		String language3 = "C LANGUAGE";
		printProgrammingLanguage(language3);
	}
}

Output:

call method java

Explanation of the example code above:

In the example above, the method we specify is called the main. It requires one argument to be passed. We executed the method four times, each time with a different parameter. The method has provided several results for different names with all four specific parameters.

Let’s look the other example:

public class Driver {

	static int addition(int a, int b) {

		int sum = a + b;
		return sum;
	}

	public static void main(String[] args) {

		int a = 64;
		int b = 75;
		int c = addition(a, b);
		System.out.println(a + " + " + b + " = " + c);

		a = 32;
		b = 45;
		c = addition(a, b);
		System.out.println(a + " + " + b + " = " + c);

		a = 67;
		b = 30;
		c = addition(a, b);
		System.out.println(a + " + " + b + " = " + c);

		a = 89;
		b = 51;
		c = addition(a, b);
		System.out.println(a + " + " + b + " = " + c);
	}
}

Output:

Call Method in Java

This is the explanation of the code above:

In the example provided above, we specify a basic addition which is called “addition“. It uses two integers, calculate the sums, and returns it as an integer. A method we usually define in the example above is called the main.

Furthermore, It requires two parameters to be provided. Particular values of a and b are provided every time as the arguments which are separated by commas. In addition, the method returns an integer value, which is put in the variable c.

In addition, we executed the method four times, each time with a different parameter. The method computed multiple total values and provided different outputs with each of the four different arguments.

Note:  That System.out.println(); is an inbuilt Java method which is the same way as the methods we required.

Conclusion

To conclude, in this tutorial after we learned on how to call a method in Java we also learned the usage of the method in Java.

Moreover, we also learn how to create, declare and call a method in Java. As a challenge, try calling multiple methods with varied parameters and return types. It will help you understand methods in Java even further.

It will improve your comprehension of Java methods. To boost your confidence in your learning, repeat the process several times.

Leave a Comment