CodeGym /Java Blog /Methods in Java /How to Call a Method in Java
Author
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

How to Call a Method in Java

Published in the Methods in Java group
Java is an object-oriented language and thus needs its methods to be defined in a class. Once a method is declared in a class it can be called in the main or any other method. There are also some built-in methods already defined in Java libraries. To call any built-in or self-defined methods using the syntax described in detail below.

What is a Method?

In Java, a method is a block of code that performs a specific function and runs only when it is called. Methods are also commonly known as functions. Each method has its name. You can pass data into a method via parameters. A method also has a return type defining the type of data it returns. According to the convention, the name of the method should be written in lowerCamelCase where the first letter should be small. Furthermore, a method should have a proper name, preferably a verb referring to what it does e.g. add(), printContactList(), updateInfo() etc. Every time a program encounters a method call, the program execution branches out to the body of the method. The body code runs and the method returns to the previous code from which it was called, and continues from the next line. A method returns to the code that invoked it when:
  1. It completes all the code in the method and reaches the end of it.
  2. It reaches a return statement.
  3. It throws an exception.

Why are Methods used?

Methods are used because they allow code to be reused without rewriting it again and again. Methods are timesavers and keep the code organized and readable. It makes the code understandable to multiple coders. It helps in modularizing the program. If methods are not used the program can become extremely lengthy and difficult to test, debug or maintain the code.

Create a Method


public class Driver {

	public static void printName(String name) {

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

Method declaration

In general, method declaration has the following components:
  1. Modifier: Defines the access type i.e. from where the method can be accessed in your program e.g. public, private, etc. It is public in this case, which means this method can be accessed outside of the class too.

  2. Return Type: The data type of the value that the method returns. In this case, it is void i.e. does not return anything.

  3. Method Name: It is the name of the method by which it will be called in our program. The name of our method is printName.

  4. Parameter List: It is the list of data that needs to be passed into the method. It is comma-separated and each input data is preceded by its datatype. If there is no data to be passed the brackets () are left empty. We have passed one parameter name of type String.

  5. Method body: It consists of the code that needs to be executed enclosed within curly braces {}.

Call a Method

To call a method in Java, simply write the method’s name followed by two parentheses () and a semicolon(;). If the method has parameters in the declaration, those parameters are passed within the parentheses () but this time without their datatypes specified. However, it is important to keep the sequence of arguments the same as defined in the method definition. Let’s look at an example to understand this better.

Example 1


public class Driver {

	public static void printName(String name) {

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

	public static void main(String[] args) {

		String name = "Mary";
		printName(name);

		String name1 = "Lucy";
		printName(name1);

		String name2 = "Alex";
		printName(name2);

		String name3 = "Zoey";
		printName(name3);
	}
}

Output

Hi, I am Mary! Hi, I am Lucy! Hi, I am Alex! Hi, I am Zoey!

Explanation

In the snippet above, the method we defined is called in the main. It has one argument that needs to be passed. We have called the method four times, each time changing the argument. With all four different arguments, the method has returned different outputs for different names.

Example 2


public class Driver {

	static int add(int x, int y) {

		int sum = x + y;
		return sum;
	}

	public static void main(String[] args) {

		int x = 10;
		int y = 20;
		int z = add(x, y);
		System.out.println(x + " + " + y + " = " + z);

		x = 5;
		y = 4;
		z = add(x, y);
		System.out.println(x + " + " + y + " = " + z);

		x = 100;
		y = 15;
		z = add(x, y);
		System.out.println(x + " + " + y + " = " + z);

		x = 50;
		y = 5;
		z = add(x, y);
		System.out.println(x + " + " + y + " = " + z);
	}
}

Output

10 + 20 = 30 5 + 4 = 9 100 + 15 = 115 50 + 5 = 55

Explanation

In the snippet above, we defined a simple addition method called “add”. It takes two integers, finds their sum, and then returns it which is also an integer. The method we defined above is called in the main. It has two arguments that need to be passed. Different values of x and y are passed each time as the arguments are separated by commas. The method also returns an integer value which is stored in the variable z. We have called the method four times, each time changing the argument. With all four different arguments, the method has computed different values of sum and returned different outputs. It is important to note that System.out.println(); is a built-in Java method that is called in the same manner as the methods we defined ourselves.

Conclusion

By now you should be familiar with methods in Java and how to call them. As a challenge, you can try calling different methods with different parameters and return types. It will further strengthen your understanding of methods in Java. To be more confident in your learning try practicing it over and over. Feel free to replug whenever you feel like it. Good luck and happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION