Recommended Books

Java Methods



This Article is about the usage of methods in Java.

Unlike other programming languages, Java does not have any functions, rather is has methods. There is a slight difference between methods and functions. Methods are always contained within classes, whereas this is not true for functions. Hence, anytime we create a method in Java, it must be within a class.

What is a method though? Methods (and functions) are a collection of statements that are grouped together to perform an operation. This group of statements does not run until they are “called”. A function is called simply by writing it’s name with any parameters it may require.

The benefit of having a group of statements is that it can be called over and over again without having to re write the code for it. This improves both code readability and saves time for the programmer.

Other programming languages have both methods and functions.


Defining Java methods

Shown below is the general form for the syntax of methods in Java.

type method_name(parameters) {
    // Instructions to be executed
}

However, there are keywords that will have to added or removed under certain scenarios. Keep in mind that all these keywords are added before the method name.

type: Always required when the function is returning a value. The type keyword is used to define the “type” of the return value. If you’re function is meant to do some calculations and return a number, the int type will be used. type keyword is not required when there is no return value.

static: Used to call a method before it’s object has been created. This will almost always be required as currently we are not using objects in our code. If you create a class object, you can call the function without using the static keyword.

void: If your function will not be returning a value, you must explicitly state this, else an error will be thrown. In simpler words, if your function does not return a value, add the void keyword before the method name.


Arguments and Parameters – The difference

These two terms are very interchangeable with each other, so knowing the difference isn’t that big a of deal. The terms they refer to are almost identical.

Typically however, the Parameters are the variables present in the parenthesis during method definition. When a method is called, the Arguments are the data you pass into the method’s parameters.

There is no limit to the number of Arguments/Parameters that can be written. There may even be no parameters at all, depending on the purpose of the function you’re making.


Methods with no arguments

Below is the basic example of a method you’ll ever see. A simple one line instruction that prints a sentence to screen. And a simple function call to execute the function. You just have the use the name of the function to call it.

public class example {

	static void Hello() {
		System.out.println("Hello world");
	}	

	public static void main(String[] args) 	{		
		Hello(); // Calls the Function
	}
}

The output of this function will be “Hello World”.


Methods with a single argument

Below is an example of a simple increment method. It takes a single number as an argument, increments it by one, and returns the value.

public class example {
	
	static int increment(int x) { 
		return x + 1;
	}
	
	public static void main(String[] args) 	{	
		System.out.println(increment(5));	
	}
}
6

Methods with multiple arguments

The method shown below is the kind of method you’ll tend to see in real life situations.

Since it’s a function dealing with integers and also returns a number, we declare it to type int. Secondly, we wish to return a value hence we remove the void keyword.

public class example {
	
	static int add(int x, int y) { 
		return x + y;
	}
	
	public static void main(String[] args) 	{	
		System.out.println(add(3,5));	
	}
}

Here, when calling the add function we added two input arguments (3 and 5) as well. It is important for the number of parameters and arguments to match. If there are three parameters, there should be three values sent as arguments. Remember to

Also remember that returning a value does not print it. That’s why we include println to display the returned values. Shown below is the displayed value of the above method.

8

This marks the end of the Java methods article. Any suggestions or contributions for CodersLegacy are more than welcome. You can ask any relevant questions in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *