Mthods in Java

A java method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform operations, and they are also known as functions.

Method Definition


modifier returntype methodname(Params) {
// method body
}
methods must be declared within classes. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as math.pow(); but you can also create your own methods to perform certain actions:

Types of Java methods

1) Standard Library Methods

The standard library methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.

For example,

  • print() is a method of java.io.PrintSteam. The print("...") method prints the string inside quotation marks.
  • sqrt() is a method of Math class. It returns the square root of a number.

Here’s a working example:

public class Main {
    public static void main(String[] args) {
    
        // using the sqrt() method
        System.out.print("Square root of 4 is: " + Math.sqrt(4));
    }
}

Output:

Square root of 4 is: 2.0

3) User-defined Method

We can also create methods of our own choice to perform some task. Such methods are called user-defined methods.

How to create a user-defined method?

Here is how we can create a method in Java:

public static void myMethod() {
    System.out.println("My Function called");
}

Here, we have created a method named myMethod(). We can see that we have used the publicstatic and void before the method name.

  • public – access modifier. It means the method can be accessed from anywhere. To learn more, visit Java access modifier
  • static – It means that the method can be accessed without any objects. To learn more, visit the Java static Keyword.
  • void – It means that the method does not return any value. We will learn more about this later in this tutorial.

This is a simple example of how we can create a method. However, the complete syntax of a method definition in Java is:

modifier static returnType nameOfMethod (parameters) {
    // method body
}

Here,

  • modifier – It defines access types whether the method is public, private and so on.
  • static – If we use the static keyword, it can be accessed without creating objects.

    For example, the sqrt() method of standard Math class is static. Hence, we can directly call Math.sqrt() without creating an instance of Math class.
  • returnType – It specifies what type of value a method returns For example if a method has int return type then it returns an integer value.

    A method can return native data types (intfloatdouble, etc), native objects (StringMapList, etc), or any other built-in and user-defined objects.

    If the method does not return a value, its return type is void.
  • nameOfMethod – It is an identifier that is used to refer to the particular method in a program.

    We can give any name to a method. However, it is more conventional to name it after the tasks it performs. For example, calculateArea()display(), and so on.
  • parameters (arguments) – These are values passed to a method. We can pass any number of arguments to a method.
  • method body – It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces { }.

How to Call a Method in Java?

In Java to use a method, you need to call it. A method is called according to its functionality. The method either returns a value or return nothing(no return value). The process of calling a method is very simple. When the program invokes any method, the control automatically gets transferred to the calling method. It is called in any of these 3 situations

  • When it completes all the statements
  • An exception is thrown to it
  • When it reaches a return statement
public class MyClass {
  static void myMethod() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

Output-

Hello World!