Java

How to Create User-Defined Methods in Java

Programmingempire

Basically, User-Defined Methods in Java help us creating a program in manageable segments. Basically, a method is a piece of code that performs awell-defined task and also has a name. Therefore, a method allow us to perform the same task in a program repeatedly without writing the code again. Further, the use of methods in a program makes the program more maintainable. Because, it is easier to update the code of a method without impacting other parts of the program.

Syntax of User-Defined Methods in Java

In order to define a method in Java, we need to use the following syntax.

[access-modifier] [other modifier] return-type method-name(arguments-list)
{
      //Statements
}

At first, we need to specify an access-modifier. For instance, we can use public or protected keyword for access-modifier. Basically an access-modifier determines in which part of the program, this method is recognised. Secondly, we can use the static modifier to define a static method. Otherwise, we can skip it for creating a non-static method. Given that, for calling a non-static method, we need to have the objects of the class. Therefore, all the examples that will follow illustrate static methods that we can call without using an object.

Furthermore, we need to specify the data type of the value returned from the method as the return-type. Otherwise, we need to use void keyword in case method doesn’t return any value. It is followed by method-name and argument-list. In fact, using the argument-list we can provide input values to a method. After that method body starts and enclosed in curly brackets which contain statements.

Calling a Method

A method can be called as given below. In case of a method returning some value, the method call should assign the returned value into a varable of appropriate data-type.

variable-name=method-name(actual-parameters);

While, if the method is not returning any value, it is simply called as given below.

method-name(parameter-list);

The following example shows a method that doesn’t return any value. Also, there is no argument-list since the method is just displaying a text string.

public static void ShowMessage(){
		System.out.println("Using Methods in Java");
	}

Another example of a method returning an integer value is given below. As can be seen the method computes the sum of its parameters and returns the sum.

public static int Compute(int a, int b){
		return a+b;
	}

The next example shows a method that finds the factorial of a number and also returns it. Further, the number is passed as a parameter to the method.

public static int factorial(int n){
		int f=1;
		for(int i=1;i<=n;i++)
		{
			f=f*i;
		}
		return f;
	}

In fact, we can also pass an array as the parameter to a method as shown in the following example that computes the sum of elements of the array.

public static int findArraySum(int[] arr){
		int sum=0;
		for(int x=0;x<arr.length;x++)
		{
			sum+=arr[x];
		}
		return sum;
	}

Moreover, a method can also return an array. The following example shows that an array is created using randomly generated numbers. Also, the same array is returned to the calling method.

public static int[] createArray(){
		int[] arr=new int[10];
		int n=10;
		
		for(int i=0;i<10;i++)
		{
			arr[i]=(int)(Math.random()*100);
		}
		return arr;
	}

The Complete Example of User-Defined Methods in Java

The following code shows the complete program using methods discussed earlier.

package BasicExamples2;
public class MethodExamples {
	public static void main(String[] args) {
		ShowMessage();
		int i=18, j=69;
		int m=Compute(i, j);
		System.out.println("Sum of "+i+" and "+j+" is "+m);
		int n=8;
		System.out.println("The factorial of "+n+" is "+factorial(n));
		int[] my_array= {12, 1, 89, 454, 67, 57};
		System.out.println("Array Elements...");
		for(int k:my_array)
		{
			System.out.print(k+" ");
		}
		System.out.println("nSum of array elements is "+findArraySum(my_array));
		System.out.println("An array using randomly generated numbers...");
		int[] array1=createArray();
		for(int k:array1)
		{
			System.out.print(k+" ");
		}
		System.out.println();
	}
	public static void ShowMessage()
	{
		System.out.println("Using Methods in Java");
	}
	public static int Compute(int a, int b)
	{
		return a+b;
	}
	public static int factorial(int n)
	{
		int f=1;
		for(int i=1;i<=n;i++)
		{
			f=f*i;
		}
		return f;
	}
	public static int findArraySum(int[] arr)
	{
		int sum=0;
		for(int x=0;x<arr.length;x++)
		{
			sum+=arr[x];
		}
		return sum;
	}
	public static int[] createArray()
	{
		int[] arr=new int[10];
		int n=10;
		
		for(int i=0;i<10;i++)
		{
			arr[i]=(int)(Math.random()*100);
		}
		return arr;
	}
}

Output

An Example of User-Defined Methods in Java
An Example of User-Defined Methods in Java

programmingempire

You may also like...