Java – Methods

A method, often known as a function, is a set of statements that runs only when called from within the programme. A method allows the same code to be reused for multiple inputs, saving time and resources. One of the most used built-in methods in Java is System.out.println(), which is used to print the output on a new line. Users can also define their own methods, referred to as user-defined methods.

Create method

A method must be declared in a class in Java. The modifier comes first, followed by the method’s return type. The method’s name is then followed by parentheses holding the method’s parameter(s) if it has any. Finally, it contains a block of statements known as the method’s body.

Syntax

//Defining method
modifier return_type method_name(parameters) {
  statements;
}


modifier: It is optional to use and determines the method’s access type. In Java, there are four different types of access modifiers.

  • public
  • protected
  • private
  • default: When no modifier is specified

The access to members granted by each modification is listed in the table below.

Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N

 

return_type: A technique can produce a result (s). The return type specifies the data type of the value returned by the method. If a method does not return anything, the return type is set to void.

Example: A method with no parameter

A method HelloWorld is built in the example below to print Hello World! The void keyword is used because it takes no parameters and has no return type.

The static keyword notifies the computer that HelloWorld is a member of the MyClass and not an object of the MyClass.

public class MyClass {
  static void HelloWorld() {
    System.out.println("Hello World!"); 
  }
  public static void main(String[] args) {
    HelloWorld();  
  }
}


 

The output of the above code will be:

Hello World!.

Call a method

Following the method’s definition, it may be called anywhere in the programme by using its name, followed by parentheses holding the method’s parameter(s), if any, and a semicolon; The function is called using the following sentence inside the main() method in the example above.

HelloWorld();

Parameter

A parameter (also known as an argument) is a type of variable that is used to transmit data to and from a method. The method in the preceding example has no parameters. A user, on the other hand, can construct a method with one or more arguments. The method can utilise the value of a parameter to get the desired outcome.

Example: A method with parameters

The method MyMethod is defined in the example below, which needs two integer numbers as parameters and prints the sum of two integers in a specified manner. Because the procedure returns nothing, the void is utilised as the return type.

public class MyClass {
  static void MyMethod(int a, int b) {
    int sum;
    sum = a + b;
    System.out.println("Sum of "+ a +" and "+ b +" is: "+ sum); 
  }
  public static void main(String[] args) {
    MyMethod(15, 10);  
  }
}


 

The output of the above code will be:

Sum of 15 and 10 is: 25

Return Values

To return values, a method can be utilised. To do this, the user must declare the return type in the method’s definition and declaration. In the example below, the return type is int.

public class MyClass {
  static int MyMethod(int a, int b) {
    return (a + b); 
  }
  public static void main(String[] args) {
    int x = 15, y = 10, sum;
    sum = MyMethod(x, y);  
    System.out.println("Sum of "+ x +" and "+ y +" is: "+ sum); 
  }
}


 

The output of the above code will be:

Sum of 15 and 10 is: 25

 

Click here to open the online java compiler.

Next: Java – Exceptions

Leave a Reply

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