Java Method

Java Method is a piece of code that performs function assigned to it. It runs only when it is called. Method is a part of java class. It is also called as function. Methods provide code reusability. There are two types of java methods:

  • User defined Methods
  • Standard Library Methods

User Defined Java Method

A Java method has following attributes:

  • Method name
  • Method return type
  • Method parameter
  • Method body
  • Method access specifier

Create a Method

Java method name is followed by parenthesis (). Parenthesis are followed by curly braces {} that contain method body. The syntax for defining a method is:

Here, methodName() is the name of method, return_type is the return type of method, access_specifier is the access modifier of method.

Call a Method

To call a java method, method name is written followed by semicolon (;). Programmer can call a method multiple times. The syntax for calling a method is:

Here is a simple method that prints Hello World!

The code shows how to create and call a java method. The method is called twice.

public class Example {
  static void greetings() {
    System.out.println("Hello World!");
  }

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

The output of the above code after compilation is:

For example, the below code shows a java method that takes sum of two integer numbers and returns the sum.

public class Example {
  static int adds(int x, int y) {
    int z = x + y;
    return z;
  }

  public static void main(String[] args) {
    System.out.println(adds(3, 7));
  }
}

Here, the method’s return type is int and it has two int parameters. The output of the above code after compilation is:

Method Name

The best practice is to keep first letter of the method name in lower case. If method name consists of more than one words then camel notation is used that is keep first letter of later words in upper case and rest of the letters in lower case. For example, myMethod(). In the above example adds() is the method name.

Method Return Type

Method’s return type defines the type of return value of the method. A method can have any of the following primitive return types like:

  • int: Method having int return type returns integer value.
  • float: Method having float return type returns float value.
  • char: Method having char return type return character value.
  • double: Method having double return type returns float value. etc.

Method can also have void return type. A method having void return type has no return value. Therefore the void method does not contain return statement. For example the main() method. Other than these return types, a method can also have object or collection return types. In the above example, method return type is int.

Method Parameter

A method may or may not have parameters. Parameters are information that is passed to a method. Parameters can be variable or constants present in the method’s parenthesis. There can be multiple parameters of a method. Each parameter is separated by the other parameter by comma (,). In the above example, the adds() method has two int parameters.

The below code shows two methods, one method takes no parameter but the other takes a single parameter.

class Example {

  // method takes no parameter
  public void method1() {
    System.out.println("This method takes no parameter");
  }

  // method with single parameter
  public void method2(int x) {
    System.out.println("This method takes a parameter: " + x);
  }

  public static void main(String[] args) {
    
    // create an object of Example
    Example obj = new Example();

    // call method with no parameter
    obj.method1();
    
    // call method with the single parameter
    obj.method2(20);
  }
}

The output of the above code after compilation is:

Method Body

The code present in curly braces {} is the method body. Method body defines the action performed by the method. Th variable declared inside the method body are not accessible outside the method. Their scope is within the method body only.

Method Access Modifier

Access modifier defines the access type of a method. It is not compulsory to write access modifier for a function. A method can have any of the modifiers defined in Java language:

  • public: Accessible form everywhere (inside and outside the class).
  • private: Accessible only inside the class.
  • protected: Accessible within the same class and sub class.
  • default: Accessible within the same package
  • static: Accessible without creating objects

Standard Library Method

Java has some built in methods. These are called standard library methods. For example:

  • print() is a built in method that prints the variable or string value in its parameters.
  • sqrt() is another built in method of Math class that takes square root of a number in its parameters.

The below uses sqrt() method to take square root of a number and then uses print() method to display the output:

public class Example {
  public static void main(String[] args) {
    
    // Take and print square root
    System.out.print("The Square root of 9 is: " + Math.sqrt(9));
  }
}

The output of the above code after compilation is: