Tutorials Hut

Tutorials Hut

Java Methods

Java method is a set or block of instructions which is written together and executes whenever someone calls it within java program, this is the simplest definition of java methods.

  • Java methods may take zero or more input arguments.
  • Java methods may return some data type but it is not mandatory.
  • Methods are attributes of any class which defines certain behaviour of it.
  • Java is made of various classes and their methods.

Syntax of Method:

<access key word> <return type> methodName(<arguments>) {
//Method body
}

How to Create Java Methods

We can define a method based on our requirement by following basic syntax of method.
Below is example of different methods created in a class:

Example:

public class MethodsExample
{
//Main method which takes String[] array as input and returns nothing (void key word)
public static void main(String[] args) {
}
//Sum methods to sum two int and return total of both, see it has a return type and also it is private, outside class cant be accessed
private int sum(int a, int b){
return a+b;
}
//Public method multiplication
public int multiplication(int a, int b) {
return a*b;
}
}

Here in above example for method named multiplication() below are details-

public: is access modifier
int: return type of method
int a, int b: are two int type parameters(arguments) of method
return a*b: is returning multiplication results of a and b.

How to Call Java Methods

We can call a method directly if within same class or create an object and can access method with help of object.

Below is an example of showing how to create and call methods:

Example:

public class MethodsExample
{
	//Main method which takes String[] array as input and returns nothing (void key word)
	public static void main(String[] args) {
    	MethodsExample methodsExample = new MethodsExample();
    	int totalSum = methodsExample.sum(10,20);
    	int multipliedValue = methodsExample.multiplication(10,20);
  	  System.out.println("totalSum by calling sum method: "+totalSum);
    	System.out.println("multipliedValue by calling multiplication method: "+multipliedValue);
	}
	//Sum methods to sum two int and return total of both, see it has a return type and also it is private, outside class cant be accessed
	private int sum(int a, int b){
    	return a+b;
	}
	//Public method multiplication
	public int multiplication(int a, int b) {
    	return a*b;
	}
}

Output:

totalSum by calling sum method: 30
multipliedValue by calling multiplication method: 200

Passing parameter by Value

In any method if it takes argument and we pass some value to those and then call the method then this is called passing parameter by value.

In above example we already saw sum and multiplication method which takes two int arguments and do operation on that this is an example of passing parameter by value.

We may also pass an object of class to a method and return and object of class.

What is return Key Word In Java

In java methods return key word is used to return any value from method, this statement is written at the end of method, if method is of void type and does not return anything we can skip it.
In below example you can see sum and multiplication methods has return key word and they are returning some int values:

Example:

public class MethodsExample
{
	//Main method which takes String[] array as input and returns nothing (void key word)
	public static void main(String[] args) {
    	MethodsExample methodsExample = new MethodsExample();
    	int totalSum = methodsExample.sum(10,20);
    	int multipliedValue = methodsExample.multiplication(10,20);
    	System.out.println("totalSum by calling sum method: "+totalSum);
    	System.out.println("multipliedValue by calling multiplication method: "+multipliedValue);
	}
	//Sum methods to sum two int and return total of both, see it has a return type and also it is private, outside class cant be accessed
	private int sum(int a, int b){
    	return a+b;
	}
	//Public method multiplication
	public int multiplication(int a, int b) {
    	return a*b;
	}
}

What is finalize() Method In Java

Finalize method is invoked before object is garbage collected.

In java there is a garbage collector (GC) which is responsible of destruction of objects from memory when not required, if we want to execute some code before garbage collection we can write code in finalize() method.

References

Reference oracle article on java methods – Java Methods

Next Article


















  • Leave a Reply

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

    Scroll to Top